//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H25.09.30 // History : // Laungage : HI_TECH PICC C Laungage Version 9.83 //****************************************************************************************** // Description // OSC : Internal OSC 16MHz // Define I/O Port : // Random OSC : RA0 // UART(RX) : RA5 // UART(TX) : RA4 //****************************************************************************************** #include __CONFIG( FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF ); __CONFIG( WRT_OFF & PLLEN_OFF & STVREN_OFF & BORV_HI & LVP_OFF ) ; // // #define PI_NORMAL 0 #define PI_FAILER 1 #define PI_TIM_NO 3 #define PI_RAND_LOOP (unsigned long)100000 // volatile unsigned char G_counter=0; volatile unsigned char G_rand_counter; void InitUART(void); char SendUART(unsigned char); char RecUART(unsigned char *); void SendUART_s(unsigned char *); unsigned char Get_rand(unsigned long); void main() { unsigned char Init_msg[]=" PIC12F1822 Connected OK> "; unsigned char tmp_land; unsigned char land_data[10]; unsigned char lp,lp1; unsigned char tmp_title; unsigned char tmp_title_data[3]; // ***************************** // 1.Initialized // ***************************** // (1)Define OSC (16MHz, IntOSC) OSCCON = 0b01111010; // 16MHz, IntOSC // (2)Define ANSELA (ALL Digital Port) ANSELA = 0x00; // (3)Define I/O port PORTA = 0b00000000; TRISA = 0b00111111; // (4)Define APFCON Register // APFCON:RXDTSEL.SDOSEL,SSSEL,---,T1GSEL,TXCKSEL,P1BSEL,CCP1SEL // BSF APFCON,RXDTSEL ; RX ==> RA5 // BSF APFCON,TXCKSEL ; TX ==> RA4 APFCONbits.RXDTSEL = 1; APFCONbits.TXCKSEL = 1; // (5)Initialized EUART InitUART(); // (6)Demo Message SendUART_s(Init_msg); // ***************************** // 2.Program main // ***************************** while(1){ // (1)Initialize SendUART(' '); for(lp=0;lp<10;lp++)land_data[lp]=0; // (2)Main process for(lp=0;lp<100;lp++){ // 2-1)Generate a random number from 0 to 9 tmp_land = Get_rand(PI_RAND_LOOP); tmp_land = tmp_land % 10 ; // 2-2)Summarize the calculated value for(lp1=0;lp1<10;lp1++) if(tmp_land == lp1)land_data[lp1]++; // 2-3)Put data tmp_land |= 0x30; SendUART(tmp_land); } // (3)Output for(lp=0;lp<10;lp++){ SendUART('['); tmp_title = lp | 0x30; SendUART(tmp_title); SendUART(']'); // tmp_title_data[0] = land_data[lp] / 10 | 0x30; tmp_title_data[1] = land_data[lp] % 10 | 0x30; SendUART(tmp_title_data[0]); SendUART(tmp_title_data[1]); SendUART(' '); } } } //****************************************************************************************** // Function Name : Get_rand // Title : // Input : // Output : //****************************************************************************************** unsigned char Get_rand(unsigned long lp_max) { unsigned long lp; unsigned char lp_counter; for(lp=0;lp is set // 0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit // Asynchronous mode 8-bit (RX9 = 0): // Donft care // RCSTA(2) FERR: Framing Error bit // 1 = Framing error (can be updated by reading RCREG register and receive next valid byte) // 0 = No framing error // RCSTA(1) OERR: Overrun Error bit // 1 = Overrun error (can be cleared by clearing bit CREN) // 0 = No overrun error // RCSTA(0) RX9D: Ninth bit of Received Data // This can be address/data bit or a parity bit and must be calculated by user firmware. // ******************************************************* RCSTA = 0b10010000; // ******************************************************* // 3.Define BAUDCON(BAUD RATE CONTROL) Register // BAUDCON(7) ABDOVF: Auto-Baud Detect Overflow bit // Asynchronous mode: // 1 = Auto-baud timer overflowed // 0 = Auto-baud timer did not overflow // BAUDCON(6) RCIDL: Receive Idle Flag bit // Asynchronous mode: // 1 = Receiver is Idle // 0 = Start bit has been detected and the receiver is active // BAUDCON(5) Unimplemented: Read as e0f // BAUDCON(4) SCKP: Synchronous Clock Polarity Select bit // Asynchronous mode: // 1 = Transmit inverted data // 0 = Transmit non-inverted data // BAUDCON(3) BRG16: 16-bit Baud Rate Generator bit // 1 = 16-bit Baud Rate Generator is used (SPBRGH:SPBRG) // 0 = 8-bit Baud Rate Generator is used (SPBRG) // BAUDCON(2) Unimplemented: Read as e0f // BAUDCON(1) WUE: Wake-up Enable bit // Asynchronous mode: // 1 = Receiver is waiting for a falling edge. No character will be received but RCIF will be set on // the falling edge. WUE will automatically clear on the rising edge. // 0 = Receiver is operating normally // BAUDCON(0) ABDEN: Auto-Baud Detect Enable bit // Asynchronous mode: // 1 = Auto-Baud Detect mode is enabled (clears when auto-baud is complete) // 0 = Auto-Baud Detect mode is disabled // ******************************************************* BAUDCON = 0b00001000; // ******************************************************* // 3.Define BRG(EUSART Baud Rate Generator Register) Register // ******************************************************* // BRG = 16000000(Hz)/4/9600(BPS)-1 ==> 0d0416 ==> 0x1A0 // (1)Define SPBRGH(EUSART Baud Rate Generator Register, High Byte) Register SPBRGH = 0x01; // (2)Define SPBRG(EUSART Baud Rate Generator Register, Low Byte) Register SPBRG = 0xA0; } //****************************************************************************************** // Program : RecUART // Title : Recive UART // Input : // Output : //****************************************************************************************** char RecUART(unsigned char *IO_data) { unsigned char status; // **************************** // 1.Check Recvive Flag // **************************** // (1)Clear RCIF PIR1 = PIR1 & 0b11111011; // (2)Check RCIF while(1){ status = PIR1 & 0x20; if(status != 0x00)break; } // **************************** // 2.Check error // **************************** status = RCSTA & 0x06; //0000,0110 RCSTA(FERR,OERR) if(status != 0x00){ *IO_data = 0x00; return(PI_FAILER); } // **************************** // 3.Set Recive Data // **************************** *IO_data = RCREG; return(PI_NORMAL); } //****************************************************************************************** // Program : SendUART // Title : Send UART // Input : // Output : //****************************************************************************************** char SendUART(unsigned char IO_data) { unsigned char status; // *************************** // 1.Check Send Flag // *************************** while(1){ status = TXSTA & 0x02; //0000,0010 TXSTA(TRMT) if(status != 0x00)break; } // *************************** // 2.Set data // *************************** TXREG = IO_data; return(PI_NORMAL); }