//****************************************************************************************** // PIC12F1822 Sample Program // Project : // Program Name : // Programer : // Create date : // History : // Laungage : XC8 C Laungage Version 2.31 //****************************************************************************************** // Description //****************************************************************************************** // #include // #pragma config FOSC = INTOSC #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config CLKOUTEN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF // #pragma config WRT = OFF #pragma config PLLEN = OFF #pragma config STVREN = OFF #pragma config BORV = HI #pragma config LVP = OFF void Wait_01S(unsigned int); void Wait_100mS(void) ; void Wait_1mS(unsigned int); void Wait_01mS(void) ; void Init_adc(void) ; unsigned int Get_adc(void) ; void main() { unsigned int lp = 0; unsigned int adc_data; // ***************************** // 1.Initialized // ***************************** // (1)Define OSC (16MHz, IntOSC) OSCCON = 0b01111010; // 16MHz, IntOSC // (2)Define ANSELA Register ANSELA |= 0b00000001; // (3)Initialized A/D converter Init_adc(); // (4)Define I/O port PORTA = 0b00000000; TRISA = 0b11101111; // ***************************** // 2.Program main // ***************************** while(1){ adc_data = Get_adc(); // PORTAbits.RA2 = 0; PORTAbits.RA4 = 0; Wait_1mS(adc_data); // PORTAbits.RA2 = 1; PORTAbits.RA4 = 1; Wait_1mS(adc_data); } } //****************************************************************************************** // Function Name : Wait_1mS // Title : This function will wait n times of 1 mS // Input : // Output : //****************************************************************************************** void Wait_1mS(unsigned int n) { unsigned int lp; for(lp=0;lp<( n * 10);lp++){ Wait_01mS(); } } //****************************************************************************************** // Function Name : Wait_01mS // Title : This function will wait 0.1mS // Input : // Output : //****************************************************************************************** void Wait_01mS(void) { // ******************************************************* // 1.Define T1CON(TIMER1 CONTROL REGISTER) Register // T1CON(7-6) TMR1CS1,TMR1CS0: Timer1 Clock Source Select bits // 11 =Timer1 clock source is Capacitive Sensing Oscillator (CAPOSC) // 10 =Timer1 clock source is pin or oscillator: // If T1OSCEN = 0: External clock from T1CKI pin (on the rising edge) // If T1OSCEN = 1: Crystal oscillator on T1OSI/T1OSO pins // 01 =Timer1 clock source is system clock (FOSC) // 00 =Timer1 clock source is instruction clock (FOSC/4) // T1CON(5-4) T1CKPS1,T1CKPS0: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale value // 10 = 1:4 Prescale value // 01 = 1:2 Prescale value // 00 = 1:1 Prescale value // T1CON(3) T1OSCEN: LP Oscillator Enable Control bit // 1 = Dedicated Timer1 oscillator circuit enabled // 0 = Dedicated Timer1 oscillator circuit disabled // T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Control bit // TMR1CS1,TMR1CS0 = 1X // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input with system clock (FOSC) // TMR1CS1,TMR1CS0 = 0X // This bit is ignored. Timer1 uses the internal clock when TMR1CS<1:0> = 1X. // T1CON(1) Unimplemented: Read as ??0?? // T1CON(0) TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // Clears Timer1 Gate flip-flop // ******************************************************* // T1CON = 0b00110001; 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.0001[S];10000Hz ) // 100mS(10Hz): 16MHz / 4(FOSC/4) / 1(Prescale) / 10000(10000Hz) ==> 0d400(Count) ==> 0x0190(Count) // TMR1 = 0xFFFF - 0x0190 ==> 0xFE6F // TMR1H = 0x3C; // TMR1L = 0xAF; TMR1H = 0xFE; TMR1L = 0xA0; // ******************************************************* // 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 ); } //****************************************************************************************** // Program : Init_adc // Title : // Input : // Output : //****************************************************************************************** void Init_adc(void) { // ******************************************************* // 1.Define ADCON0(A/D CONTROL REGISTER 0) Register // ADCON0(7) Unimplemented: Read as '0' // ADCON0(6-2) CHS<4:0>: Analog Channel Select bits // 00000 = AN0 // 00001 = AN1 // 00010 = AN2 // 00011 = AN3 // 00100 = AN4(1) PIC16F/LF1823 only. For PIC12F/LF1822 it is 'Reserved. No channel connected' // 00101 = AN5(1) PIC16F/LF1823 only. For PIC12F/LF1822 it is 'Reserved. No channel connected' // 00110 = AN6(1) PIC16F/LF1823 only. For PIC12F/LF1822 it is 'Reserved. No channel connected' // 00111 = AN7(1) PIC16F/LF1823 only. For PIC12F/LF1822 it is 'Reserved. No channel connected' // 10001 = Reserved // . // . // . // 11101 = Reserved // 11110 = DAC // 11111 = FVR // ADCON0(1) GO/DONE: A/D Conversion Status bit // 1 = A/D conversion cycle in progress. Setting this bit starts an A/D conversion cycle. // This bit is automatically cleared by hardware when the A/D conversion has completed. // 0 = A/D conversion completed/not in progress // ADCON0(0) ADON: ADC Enable bit // 1 = ADC is enabled // 0 = ADC is disabled and consumes no operating current // ******************************************************* ADCON0 = 0b00000001 ; // ******************************************************* // 2.Define ADCON1(A/D CONTROL REGISTER 1) Register // ADCON1(7) ADFM: A/D Result Format Select bit // 1 = Right justified. Six Most Significant bits of ADRESH are set to '0' when the conversion result is loaded. // 0 = Left justified. Six Least Significant bits of ADRESL are set to '0' when the conversion result is loaded. // ADCON1(6-4) ADCS<2:0>: A/D Conversion Clock Select bits // 000 = FOSC/2 // 001 = FOSC/8 // 010 = FOSC/32 // 011 = FRC (clock supplied from a dedicated RC oscillator) // 100 = FOSC/4 // 101 = FOSC/16 // 110 = FOSC/64 // 111 = FRC (clock supplied from a dedicated RC oscillator) // ADCON1(3-2) Unimplemented: Read as '0' // ADCON1(1-0) ADPREF<1:0>: A/D Positive Voltage Reference Configuration bits // 00 = VREF+ is connected to AVDD // 01 = Reserved // 10 = VREF+ is connected to external VREF+ // 11 = VREF+ is connected to internal fixed voltage reference // ******************************************************* ADCON1 = 0b10010000; } //****************************************************************************************** // Function Name : Get_adc // Title : // Input : // Output : ADDAT_H,ADDAT_L //;****************************************************************************************** unsigned int Get_adc(void) { unsigned int adc_data= 0; // ***************** // 1.Start A/D Converter // ***************** // (1)GO/DONE: A/D Conversion Status bit ADCON0 |= 0b00000010; // ***************** // 2.Get data // ***************** // (1)Wait for the adc start flag while((ADCON0 & 0b00000010) != 0); // (2)Substitute the result adc_data = ( ADRESH << 8 ) + ADRESL ; return(adc_data); }