//****************************************************************************************** // 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_NUM 9 #define PI_RAND_LOOP (unsigned long)50000 #define PI_GET_NUM 8 // 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); unsigned long pow10(unsigned long); unsigned long Get_rand_degit(unsigned char); void main() { // unsigned char Init_msg[]=" PIC12F1822 Connected OK> "; unsigned char Init_msg[]=" Input n (aaaaaaaa)? "; unsigned char Init_msg_1[]=" OK "; unsigned char tmp_land; unsigned long land_long; unsigned long tmp_land_long; unsigned long tmp_index; unsigned int get_rand; unsigned char lp; unsigned char tmp_char; // ***************************** // 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 // Random number between 0 and (10^PI_NUM - 1) is created // ***************************** // ---------------------------------- // A.Enter a range of random numbers // ---------------------------------- get_rand = 0; for(lp=0;lp tmp_index * 9){ tmp_land_long = tmp_land_long % ( tmp_index * 10 ); } if(tmp_land_long < tmp_index ){ tmp_char = 0; }else{ tmp_char = tmp_land_long / tmp_index ; } tmp_char |= 0x30; SendUART(tmp_char); tmp_land_long = tmp_land_long % tmp_index; } // 3-3)Display on the screen space SendUART(' '); } } //****************************************************************************************** // Function Name : Get_rand_degit // Title : This function obtains a random n-digit // Input : Digit of data // Output : //****************************************************************************************** unsigned long Get_rand_degit(unsigned char in_deg) { unsigned long land_long=0; unsigned char lp; unsigned char tmp_land; 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); }