//Writing and Reading from Compact Flash Card using UART communication #include #include #include #include #define begin { #define end } //-------------------------------------Jae's definitions //reseting ALFAT #define ALFATresetPin PORTD.5 //setting CTS pin of ALFAT //1 if mega32 is sending data //0 if mega32 is ready to receive #define Sending() PORTD.4= 1 #define ReadytoRC() PORTD.4= 0 //checking RTS pin of ALFAT //1 if ALFAT buffer is filled #define Full() PIND.6 //---------------------------------------Song's definitions #define LCDwidth 16 #define musicOnly 0 //L+R #define KROKonly 1 //L-R #define KROKwithStore 2 //record L+V #define KROKwithVocRm 3 //record L-R #define KROKfull 4 //record L-R+V #define voiceRecord 5 //record V #define playback 6 //playback #define NCS PORTB.4 //The spi control command to ADC #define leftChCtrl1 0b01101100 #define leftChCtrl2 0b00000000 #define rightChCtrl1 0b01101000 #define rightChCtrl2 0b00000000 #define voiceCtrl1 0b01100000 #define voiceCtrl2 0b00000000 /* Use an 1x16 alphanumeric LCD connected to PORTC as follows: [LCD] [Mega32 pin] 1 GND- GND 2 +5V- VCC 3 VLC 10k trimpot wiper (trimpot ends go to +5 and gnd) 4 RS - PC0 5 RD - PC1 6 EN - PC2 11 D4 - PC4 12 D5 - PC5 13 D6 - PC6 14 D7 - PC7 */ // initialize the LCD for 16 char wide #asm .equ __lcd_port=0x15 #endasm #include // LCD driver routines /* MCP3204 connections to MEGA32 14 Vin - 5V 13 Vref - 5V 12 AGND - 20 GNG 11 SCLK - 8 SCK PORTB7 10 DOUT - 7 MISO PORTB6 9 DIN - 6 MOSI PORTB5 8 NCS - 5 NSS PORTB4 7 DGND - 20 GND 6 NC 6 NC 4 CH3 - Left Channel 3 CH2 - Right Channel 2 CH1 - No Connection 1 CH0 - Microphone */ //---------------------------------------------- void InitializeALFAT(void); //Initializing ALFAT void ErrorHandler(unsigned char a); //when error prints data to hyperterm unsigned char receiving(void); //receiving data void transmitting(unsigned char data); //transmitting one byte void transFrame(unsigned char * frame, unsigned char st); //transmitting an array data (commands) unsigned char Er(void); //checking ACK void writing(void); //initializing writing mode void reading(void); //initializing reading mode void close(unsigned char fileNum); //closing the file that is opened void storing(unsigned char dt); //storing data into Compact Flash void BaudtransFrame(unsigned char * frame, unsigned char st); //transmitting Baudrate packet command unsigned char checking(void); //checking ACK //--------------------------------------------- void Initialize(void); //Initializations void LCDUpdate(void); //update the LCD void ButtonsUpdate(void); //update the buttons unsigned char adcRead(void); //adc conversion //---------------------------------------------Song's variables char lcdBuffer[17]; //LCD display buffer char KROKmode; //mode of operation char adcResult; //result acquired from ADC char buttonPressed; //flag on whether button is on hold int tLCD,tButtons; //time bases //---------------------------------------------Jae's variables //time delay unsigned int i; //giving time delay unsigned int counter1, counter2; //counters in writing mode //RX variables unsigned char ACK; //input string unsigned char r_d; //receiving one byte(used to clear receiving buffer) unsigned char r_length; //length of body + checksum unsigned char readingdata[16]; //stores reading data (buffer) unsigned char c, t; //for reading //TX variables unsigned char stop; //show at which index to stop transmit unsigned char data[16]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //data (used for initializing) unsigned char t_data; //sending one byte (used for sending AD data) unsigned int data_length; //total length of data unsigned char flushdata[4]= {0x82, 0x0e, 1, 0x91}; //flush data command unsigned char appenddata[7]= {0xc5, 0xe8, 0x03, 0, 0, 1, 0xb1}; //append command //flags unsigned char wflag, rflag; //********************************************************** //UART character-ready ISR interrupt [USART_RXC] void uart_rec(void) begin readingdata[c] = UDR; //receiving data from ALFAT if(c==0) begin Sending(); //CTS high to stop ALFAT OCR0=38; //about 100 uSec TCCR0=0b00001011; //prescalar to 64 and Clr-on-match TIMSK=2; //turn on timer 0 cmp-match ISR end c= c+1; //increment index of reading buffer end //---------------------------------------------interrupt interrupt [TIM0_COMP] void timer0_compare(void) begin //reading mode if(rflag && KROKmode == playback) begin if(t < c) begin //outputs to PORTA PORTA= readingdata[t]; t= t+1; end if(t == c) begin //resets indexs t= 0; c= 0; //stops timer0 and init RXC interrupt TIMSK= 0; UCSRB.7= 1; //activates ALFAT ReadytoRC(); end end //writing mode else begin adcResult=adcRead(); if(wflag) storing(adcResult); PORTA=adcResult; end if (tLCD != 0) tLCD--; if (tButtons != 0) tButtons--; end //---------------------------------------------functions //Entry point and task scheduler loop void main(void) begin //for reading c= 0; t= 0; //for writing counter1= 0; counter2= 0; Initialize(); //Initialize the SPI, button, and LCD InitializeALFAT(); //Initialize the CompactFlash OCR0=33; //about 100 uSec TCCR0=0b00001011; //prescalar to 64 and Clr-on-match TIMSK=2; //turn on timer 0 cmp-match ISR while (1) begin //update LCD if (tLCD == 0) LCDUpdate(); //check button if (tButtons == 0) ButtonsUpdate(); end end //Set it all up void InitializeALFAT(void) begin //setting up the transmitting setup UCSRB = 0b00011000; //enable transmit and receive UCSRC = 0b10000110; //data size of 8bits UBRRL = 95; //baud rate 9600bps //PIN setup DDRD.2 = 0; //input from a writing button #0 DDRD.3 = 0; //input from a reading button #1 DDRD.4 = 1; //CTS signing pin DDRD.5 = 1; //reset pin for ALFAT //init t_data and flag (testing issue) t_data= 0x40; rflag= 0; wflag= 0; //reseting ALFAT ALFATresetPin=0; for(i=0;i<10000;i++); ALFATresetPin=1; for(i=0;i<10000;i++); //receiving BL r_d= receiving(); r_d= receiving(); UCSRB.4=0; //transmitting R transmitting('R'); //receiving ...HI.... UCSRB.4=1; while(receiving()!='H'); while(receiving()!='I'); UCSRB.4=0; for(i=0; i< 60000;i++); //give delay prepare ALFAT //transmitting FM/r transmitting('F'); for(i=0; i< 10000;i++); transmitting('M'); for(i=0; i< 10000;i++); transmitting(0x0d); for(i=0; i< 10000;i++); //Changing Drive to B: //function header = 0xE1, function body = 0x42, check sum= 0x23 data[0]= 0xe1; data[1]= 0x42; data[2]= 0xe1 + 0x42; stop= 3; transFrame(data, stop); //change baud rate to 230400 //function header= 0b10000000, function body = 0x0B(ALFAT command), divider = 15 (0x0F) data[0]= 0x83; data[1]= 0x0b; data[2]= 0x10; data[3]= 0x00; data[4]= data[0] + data[1] + data[2] + data[3]; stop= 5; BaudtransFrame(data, stop); for(i=0; i< 10000;i++); UBRRL = 3; //new baud rate 230400bps //receiving ACK if (Er()); //crank up the ISRs #asm sei #endasm end //writing mode void writing(void) begin //Open a file //function header= 0b10001000, function body = 0x04(ALFAT command), 0x77(Open mode), 0x01(file handle), 0x77('w'), 0x2E ('.'), 0x54('T'), 0x58('X'), 0x54('T'), check sum= data[0]= 0x87; data[1]= 0x04; data[2]= 0x77; data[3]= 0x01; data[4]= 0x77; data[5]= 0x2E; data[6]= 0x54; data[7]= 0x58; data[8]= data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]; stop= 9; transFrame(data, stop); close(1); //Open a file to append //function header= 0b10001000, function body = 0x04(ALFAT command), 0x61(Open mode), 0x01(file handle), 0x77('w'), 0x2E ('.'), 0x54('T'), 0x58('X'), 0x54('T'), check sum= data[0]= 0x89; data[1]= 0x04; data[2]= 0x61; data[3]= 0x01; data[4]= 0x77; data[5]= 0x2E; data[6]= 0x54; data[7]= 0x58; data[8]= 0x54; data[9]= 0x00; data[10]= data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7] + data[8]; stop= 11; transFrame(data, stop); //Init. write to the file, 'W'. //function header= 0xc5, function body = 0x1a, 0x00, 0x00, 0x00, 0x01 data[0]= 0xc5; data[1]= 0xe8; data[2]= 0x03; data[3]= 0x00; data[4]= 0x00; data[5]= 0x01; data[6]= data[0] + data[1] + data[2] + data[3] + data[4] + data[5]; stop= 7; data_length= 1000; transFrame(data, stop); wflag= 1; end //reading mode void reading(void) begin //turns off interrupt TIMSK= 0; //open the file //function header= 0b10001000, function body = 0x04(ALFAT command), 0x72(Open mode), 0x06(file handle), 0x42('B'), 0x2E ('.'), 0x54('T'), 0x58('X'), 0x54('T'), check sum= data[0]= 0x87; data[1]= 0x04; data[2]= 0x72; data[3]= 0x06; data[4]= 0x77; data[5]= 0x2E; data[6]= 0x54; data[7]= 0x58; data[8]= data[0] + data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]; stop= 9; transFrame(data, stop); //Read file //function header= 0xa5, body = 0x1a, filehandle = 6 data[0]= 0xa5; data[1]= 0xa0; data[2]= 0x86; data[3]= 0x01; data[4]= 0x00; data[5]= 0x06; data[6]= data[0] + data[1] + data[2] + data[3] + data[4] + data[5]; stop= 7; data_length= 100000; transFrame(data, stop); Sending(); rflag= 1; UCSRB.4= 1; UCSRB.7=1; ReadytoRC(); end //closing file void close(unsigned char fileNum) begin data[0]= 0x82; data[1]= 0x05; data[2]= fileNum; data[3]= data[0] + data[1] + data[2]; stop= 4; transFrame(data, stop); end //checks for ACK and receives all packet unsigned char Er(void) begin UCSRB.4= 1; ACK= receiving(); r_length= (0x1f&ACK)+1; if((ACK >> 4)==0x02) begin while(r_length != 0) begin r_length= r_length - 1; r_d= receiving(); end return 0; end UCSRB.4= 0; return 1; end //stop the system and light led corresponding the input void ErrorHandler(unsigned char a) begin //printing error while(1) begin PORTC= ~a; end end //sending one byte at a time void transmitting(unsigned char data) begin while(!(UCSRA.5)); UDR= data; end //sending an array of data void transFrame(unsigned char * frame, unsigned char st) begin unsigned char i; unsigned char re= 1; while(re) begin delay_us(100); i=0; while(i < st) begin while(!(UCSRA.5)); UDR= frame[i]; i= i+1; end re= checking(); end end //receiving one byte of data unsigned char receiving(void) begin while(!(UCSRA.7)); return UDR; end //********************************************************** void storing(unsigned char dt) begin unsigned char tt; unsigned char j; tt= 1; if(counter1< 1000) begin transmitting(dt); //sends data counter2= counter2 + 1; //data sent counter if(counter2==1000) begin Sending(); for(j=0; j<10; j++) transmitting(0x00); ReadytoRC(); UCSRB.4= 1; while(tt) begin ACK= receiving(); tt= !(ACK==0x24); end r_length= (0x1f & ACK)+1; while(r_length > 0) begin r_length= r_length - 1; r_d= receiving(); end UCSRB.4= 0; //for(i=0; i< 10000;i++); transFrame(flushdata, 4); counter1= counter1 + 1; counter2= 0; if(counter1 < 1000) transFrame(appenddata, 7); end end end //checking ack unsigned char checking(void) begin UCSRB.4= 1; ACK= receiving(); r_length= (0x1f & ACK)+1; while(r_length > 0) begin r_length= r_length - 1; r_d= receiving(); end UCSRB.4= 0; if((ACK >> 4)==0x02) return 0; else return 1; end //sending an array of data void BaudtransFrame(unsigned char * frame, unsigned char st) begin unsigned char k= 0; while(k < st) begin while(!(UCSRA.5)); UDR= frame[k]; k++; end end //----------------------------------------------------------Song's functions //********************************************************** //Initialization //********************************************************** void Initialize(void) begin DDRA=0xff; //Music out KROKmode=0; DDRB=0b10110000; //Set up the SPI port SPCR=0b01011101; //initialize the SPI in master mode SPSR.0=1; //no interrupts, MSB first, clock phase negative //SCK high when idle, clock phase=0 //SCK=fxtal/8=2MHz tLCD=250; //Initialize the timer bases tButtons=2500; buttonPressed = 0; lcd_init(LCDwidth); //initialize the display lcd_clear(); //clear the display lcd_gotoxy(0,0); lcd_putsf("KROK Machine"); lcd_gotoxy(0,1); lcd_puts(lcdBuffer); end //********************************************************** //ADC read routine //********************************************************** unsigned char adcRead(void) begin unsigned char adcOut1,adcOut2; unsigned char musicR,musicL,vocal; NCS=0; //enable the ADC switch(KROKmode) begin case musicOnly: adcOut1 = spi(voiceCtrl1); //acquire music/voice adcOut2 = spi(voiceCtrl2); NCS=1; //disable ADC return adcOut2; case KROKonly: adcOut1 = spi(rightChCtrl1); //acquire the right channel adcOut2 = spi(rightChCtrl2); NCS=1; //disable ADC musicR = (adcOut2>>1) + 64; NCS=0; adcOut1 = spi(leftChCtrl1); //acquire the left channel adcOut2 = spi(leftChCtrl2); NCS=1; //disable ADC musicL = (adcOut2>>1); return (musicR-musicL)+(musicR-musicL); case KROKwithStore: adcOut1 = spi(leftChCtrl1); //acquire the left channel adcOut2 = spi(leftChCtrl2); NCS=1; //disable ADC musicL=(adcOut2>>1); NCS=0; adcOut1 = spi(voiceCtrl1); //acquire the voice channel adcOut2 = spi(voiceCtrl2); NCS=1; //disable ADC vocal=adcOut2; return musicL+vocal; case KROKwithVocRm: adcOut1 = spi(rightChCtrl1); //acquire the right channel adcOut2 = spi(rightChCtrl2); NCS=1; //disable ADC musicR = (adcOut2>>1) + 64; NCS=0; adcOut1 = spi(leftChCtrl1); //acquire the left channel adcOut2 = spi(leftChCtrl2); NCS=1; musicL = (adcOut2>>1); return (musicR-musicL)+(musicR-musicL); case KROKfull: adcOut1 = spi(rightChCtrl1); //acquire the left channel adcOut2 = spi(rightChCtrl2); NCS=1; //disable ADC musicR = (adcOut2>>1) + 64; NCS=0; adcOut1 = spi(leftChCtrl1); //acquire the right channel adcOut2 = spi(leftChCtrl2); NCS=1; //disable ADC musicL = (adcOut2>>1); NCS=0; adcOut1 = spi(voiceCtrl1); //acquire the voice channel adcOut2 = spi(voiceCtrl2); NCS=1; return (musicR-musicL)+(musicR-musicL)+adcOut2; case voiceRecord: adcOut1 = spi(voiceCtrl1); //acquire the voice channel adcOut2 = spi(voiceCtrl2); NCS=1; //disable ADC return adcOut2; case playback: break; end end //********************************************************** //Buttons update routine //********************************************************** void ButtonsUpdate(void) begin tButtons=2500; //reset the timer if ( ((~PINB) & 0b00001111) == 0 ) buttonPressed = 0; //reset the button flag if (buttonPressed == 0) begin if ( ((~PINB) & 0b00001000) != 0 ) //button 3 is pressed begin KROKmode=KROKwithVocRm; buttonPressed = 1; //set the button flag writing(); end if ( ((~PINB) & 0b00000100) != 0 ) //button 2 is pressed begin KROKmode=KROKfull; buttonPressed = 1; //set the button flag writing(); end if ( ((~PINB) & 0b00000010) != 0 ) //button 1 is pressed begin KROKmode=voiceRecord; buttonPressed = 1; //set the button flag writing(); end if ( ((~PINB) & 0b00000001) != 0 ) //button 0 is pressed begin KROKmode=playback; buttonPressed = 1; //set the button flag reading(); end end end //********************************************************** //LCD update routine //********************************************************** void LCDUpdate(void) begin tLCD=2500; //reset the timer switch (KROKmode) //Display msg according to mode begin case 0: sprintf(lcdBuffer,"Music Playing "); break; case 3: sprintf(lcdBuffer,"Vocal Removal "); break; case 4: sprintf(lcdBuffer,"full Karaoke "); break; case 5: sprintf(lcdBuffer,"Voice Recorder"); break; case 6: sprintf(lcdBuffer,"Playback "); break; end lcd_gotoxy(0,1); //go to 2nd of the LCD lcd_puts(lcdBuffer); //Display the current mode end