Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <stdlib.h>
- #include "uart.h"
- #define F_CPU 16000000
- #include <util/delay.h>
- #include "switch.h"
- unsigned short checksum;
- /*
- $S = start code - always 0x7E
- cmd = unique command
- feedback = whether feedback is to be sent - either 0 or 1
- param1 = first parameter, if any
- param2 = second parameter, if any
- checksum1 = first byte of checksum
- checksum2 = second byte of checksum
- $0 = end code - always 0xEF
- */
- // Command-array: [$S, cmd, feedback, param1, param2, checksum1, checksum2, $0]
- unsigned char reset[] = { 0x7E, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
- unsigned char source[] = { 0x7E, 0x09, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
- unsigned char play[] = { 0x7E, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
- unsigned char volume[] = { 0x7E, 0x06, 0x00, 0x00, 0x14, 0x00, 0x00, 0xEF };
- unsigned char playTrack[] = { 0x7E, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
- unsigned char stop[] = { 0x7E, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF };
- unsigned char specify[] = { 0x7E, 0x0F, 0x00, 0x00, 0x02, 0x00, 0x00, 0xEF };
- unsigned char next [] = {0x7E, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
- unsigned char volumeDown[] = {0x7E, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF};
- // Returns the checksum in a 16-bit unsigned short
- unsigned short checksumCalc(unsigned char CMD, unsigned char feedback, unsigned char para1, unsigned char para2) {
- return (0xFFFF - ((unsigned short)CMD + (unsigned short)feedback + (unsigned short)para1 + (unsigned short)para2) + 1);
- }
- void initSomo(void) {
- //Reset[] checksum calculation
- checksum = checksumCalc(reset[1], reset[2], reset[3], reset[4]);
- reset[5] = (unsigned char)(checksum >> 8);
- reset[6] = (unsigned char)checksum;
- // source[] checksum calculation
- checksum = checksumCalc(source[1], source[2], source[3], source[4]);
- source[5] = (unsigned char)(checksum >> 8);
- source[6] = (unsigned char)checksum;
- // play[] checksum calculation
- checksum = checksumCalc(play[1], play[2], play[3], play[4]);
- play[5] = (unsigned char)(checksum >> 8);
- play[6] = (unsigned char)checksum;
- // volume[] checksum calculation
- checksum = checksumCalc(volume[1], volume[2], volume[3], volume[4]);
- volume[5] = (unsigned char)(checksum >> 8);
- volume[6] = (unsigned char)checksum;
- // playTrack[] checksum calculation
- checksum = checksumCalc(playTrack[1], playTrack[2], playTrack[3], playTrack[4]);
- playTrack[5] = (unsigned char)(checksum >> 8);
- playTrack[6] = (unsigned char)checksum;
- // stop[] checksum calculation
- checksum = checksumCalc(stop[1], stop[2], stop[3], stop[4]);
- stop[5] = (unsigned char)(checksum >> 8);
- stop[6] = (unsigned char)checksum;
- // specify[] checksum calculation
- checksum = checksumCalc(specify[1], specify[2], specify[3], specify[4]);
- specify[5] = (unsigned char)(checksum >> 8);
- specify[6] = (unsigned char)checksum;
- //Next checksum calculation
- checksum = checksumCalc(next[1], next[2], next[3], next[4]);
- next[5] = (unsigned char)(checksum >> 8);
- next[6] = (unsigned char)checksum;
- //Volume down calculation
- checksum = checksumCalc(volumeDown[1], volumeDown[2], volumeDown[3], volumeDown[4]);
- volumeDown[5] = (unsigned char)(checksum >> 8);
- volumeDown[6] = (unsigned char)checksum;
- InitUART(9600, 8, 'N', 0);
- }
- void spilsang() //Modtager 0x01 =startmelodi, 0x02 = reflekslyd eller 0x03 = slutmelodi og afspiller
- {
- for (int i = 0; i < 8; i++)
- {
- SendChar(playTrack[i]);
- }
- _delay_ms(100);
- }
- int main(void)
- {
- initSomo();
- initSwitchPort();
- while (1)
- {
- if (switchOn(7))//stop
- {
- for (int i = 0; i < 8; i++)
- {
- SendChar(stop[i]);
- }
- }
- if (switchOn(6)) //next
- {
- for (int i = 0; i < 8; i++)
- {
- SendChar(next[i]);
- }
- _delay_ms(500);
- }
- if (switchOn(5)) //volume 20
- {
- for (int i = 0; i < 8; i++)
- {
- SendChar(volume[i]);
- }
- }
- if (switchOn(4)) //volume--
- {
- for (int i = 0; i < 8; i++)
- {
- SendChar(volumeDown[i]);
- }
- _delay_ms(100);
- }
- if (switchOn(3)) //afspil track #2.
- {
- spilsang();
- }
- if (switchOn(2)) //afspil track #1
- {
- spilsang();
- }
- if (switchOn(1)) //afspil track #3.
- {
- spilsang();
- }
- //End of while
- }
- //End of main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement