//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H25.10.06 // Replace date : // History : // Laungage : HI_TECH PICC C Laungage Version 9.83 //****************************************************************************************** // Description // OSC : Internal OSC 16MHz //****************************************************************************************** #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_RAND_LOOP (unsigned long)50000 void timer(unsigned int); void M0puls(void); void M1puls(void); void M_hyp(unsigned int); void M_dot(unsigned int); void M_spe(unsigned int); void M_end(unsigned int); void M_out(unsigned char); void Mintim(void); unsigned char Get_char(void); unsigned char Get_rand(void); void InitUART(void); char SendUART(unsigned char); char RecUART(unsigned char *); void SendUART_s(unsigned char *); void Strcpy(unsigned char *,unsigned char *); int main(void) { // unsigned char Init_msg[]="OK> "; unsigned char out_char; // ******************************** // 1.Initialized Segment // ******************************** // (1)Define OSC (Internal OSC 16MHz) OSCCON = 0b01111010; // (2)Define ANSELA (All Degital Port) ANSELA = 0x00; // (3)Define I/O PORT PORTA = 0x00; TRISA = 0b11111101; // (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); SendUART('>'); // ******************************** // 2.Program main // ******************************** while(1){ out_char = Get_char(); M_out(out_char); SendUART(out_char); } } //**************************************************************************** // Function Name : M_outs // Title : // Input : // Output : // Description : //**************************************************************************** unsigned char Get_char(void) { unsigned char M_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; unsigned char data_index; data_index = Get_rand(); data_index = data_index % 36; return(M_data[data_index]); } //****************************************************************************************** // Function Name : Get_rand // Title : // Input : // Output : //****************************************************************************************** unsigned char Get_rand(void) { unsigned long lp; unsigned char lp_counter; for(lp=0;lp = 1X. // T1CON(1) Unimplemented: Read as e0f // T1CON(0) TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // Clears Timer1 Gate flip-flop // ******************************************************* T1CON = 0b00000001; // ******************************************************* // 2.Define T1GCON (TIMER1 GATE CONTROL REGISTER) Register // T1GCON(7) TMR1GE: Timer1 Gate Enable bit // If TMR1ON = 0: // This bit is ignored // If TMR1ON = 1: // 1 = Timer1 counting is controlled by the Timer1 gate function // 0 = Timer1 counts regardless of Timer1 gate function // T1GCON(6) T1GPOL: Timer1 Gate Polarity bit // 1 = Timer1 gate is active-high (Timer1 counts when gate is high) // 0 = Timer1 gate is active-low (Timer1 counts when gate is low) // T1GCON(5) T1GTM: Timer1 Gate Toggle Mode bit // 1 = Timer1 Gate Toggle mode is enabled // 0 = Timer1 Gate Toggle mode is disabled and toggle flip-flop is cleared // Timer1 gate flip-flop toggles on every rising edge. // T1GCON(4) T1GSPM: Timer1 Gate Single-Pulse Mode bit // 1 = Timer1 gate Single-Pulse mode is enabled and is controlling Timer1 gate // 0 = Timer1 gate Single-Pulse mode is disabled // T1GCON(3) T1GGO/DONE: Timer1 Gate Single-Pulse Acquisition Status bit // 1 = Timer1 gate single-pulse acquisition is ready, waiting for an edge // 0 = Timer1 gate single-pulse acquisition has completed or has not been started // This bit is automatically cleared when T1GSPM is cleared. // T1GCON(2) T1GVAL: Timer1 Gate Current State bit // Indicates the current state of the Timer1 gate that could be provided to TMR1H:TMR1L. // Unaffected by Timer1 Gate Enable (TMR1GE). // T1GCON(1-0) T1GSS1,T1GSS0: Timer1 Gate Source Select bits // 00 = Timer1 Gate pin // 01 = Timer0 overflow output // 10 = Comparator 1 optionally synchronized output (SYNCC1OUT) // 11 = Comparator 2 optionally synchronized output (SYNCC2OUT) // ******************************************************* T1GCON = 0b00000000; // ******************************************************* // 3.Define TMR1 Register // ******************************************************* // TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.01[S];100Hz ) // 10mS(100Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 100(100Hz)==> 0d5,000(Count) ==> 0x1388(Count) // TMR1 = 0xFFFF - 0x1388 ==> 0xEC77 // TMR1 data ( OSC:16MHz, FOSC/4, 1:8 Prescale value, 0.1[S];10Hz ) // 100mS(10Hz): 16MHz / 4(FOSC/4) / 8(Prescale) / 10(10Hz) ==> 0d50,000(Count) ==> 0xC350(Count) // TMR1 = 0xFFFF - 0xC350 ==> 0x3CAF // TMR1 data ( OSC:16MHz, FOSC/4, 1:1 Prescale value, 0.000892857[S];1120Hz ) // 100mS(10Hz): 16MHz / 4(FOSC/4) / 1(Prescale) / 1120(1120Hz) ==> 0d3571(Count) ==> 0x0DF3(Count) // TMR1 = 0xFFFF - 0x0DF3 ==> 0xF20C TMR1H = 0xF2; TMR1L = 0x0C; // ******************************************************* // 4.Check Timer1 // PIR1: PERIPHERAL INTERRUPT REQUEST REGISTER 1 // PIR1[7] TMR1GIF: Timer1 Gate Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[6] ADIF: A/D Converter Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[5] RCIF: USART Receive Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[4] TXIF: USART Transmit Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[3] SSP1IF: Synchronous Serial Port (MSSP) Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[2] CCP1IF: CCP1 Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[1] TMR2IF: Timer2 to PR2 Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // PIR1[0] TMR1IF: Timer1 Overflow Interrupt Flag bit // 1 = Interrupt is pending / 0 = Interrupt is not pending // ******************************************************* // (1)Clear the TMR1IF Flag bit PIR1 &= 0b11111110; // (2)Check the TMR1IF Flag bit while((PIR1 &= 0b00000001) == 0 ); } //**************************************************************************** // Function Name : Strcpy // Title : // Input : // Output : // Description : //**************************************************************************** void Strcpy(unsigned char out[],unsigned char in[]) { unsigned int lp=0; while(in[lp]!=0x00){ out[lp]=in[lp]; lp++; } out[lp]=0x00; } //****************************************************************************************** // Function Name : SendUART_s // Title : Send UART String // Input : // Output : //****************************************************************************************** void SendUART_s(char *put_string) { while(*put_string!=0x00){ SendUART(*put_string); put_string++; } } //****************************************************************************************** // Program : InitUART // Title : Initialize UART // Input : // Output : // Description : Fosc:16MHz, Baud Rate:9600BPS, 16-bit Baud Rate //****************************************************************************************** void InitUART(void){ // ******************************************************* // 1.Define TXSTA(TRANSMIT STATUS AND CONTROL) Register // TXSTA(7) CSRC: Clock Source Select bit // Asynchronous mode:Donft care // TXSTA(6) TX9: 9-bit Transmit Enable bit // 1 = Selects 9-bit transmission // 0 = Selects 8-bit transmission // TXSTA(5) TXEN: Transmit Enable bit(1) // 1 = Transmit enabled // 0 = Transmit disabled // TXSTA(4) SYNC: EUSART Mode Select bit // 1 = Synchronous mode // 0 = Asynchronous mode // TXSTA(3) SENDB: Send Break Character bit // Asynchronous mode: // 1 = Send Sync Break on next transmission (cleared by hardware upon completion) // 0 = Sync Break transmission completed // TXSTA(2) BRGH: High Baud Rate Select bit // Asynchronous mode: // 1 = High speed // 0 = Low speed // TXSTA(1) TRMT: Transmit Shift Register Status bit // 1 = TSR empty // 0 = TSR full // TXSTA(0) TX9D: Ninth bit of Transmit Data // Can be address/data bit or a parity bit. // ******************************************************* TXSTA = 0b00100100; // ******************************************************* // 2.Define RCSTA(RECEIVE STATUS AND CONTROL) Register // RCSTA(7) SPEN: Serial Port Enable bit // 1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins) // 0 = Serial port disabled (held in Reset) // RCSTA(6) RX9: 9-bit Receive Enable bit // 1 = Selects 9-bit reception // 0 = Selects 8-bit reception // RCSTA(5) SREN: Single Receive Enable bit // Asynchronous mode: // Donft care // RCSTA(4) CREN: Continuous Receive Enable bit // Asynchronous mode: // 1 = Enables receiver // 0 = Disables receiver // RCSTA(3) ADDEN: Address Detect Enable bit // Asynchronous mode 9-bit (RX9 = 1): // 1 = Enables address detection, enable interrupt and load the receive buffer when RSR<8> 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); }