//****************************************************************************************** // 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_01nmS(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; unsigned int adc_data_L; unsigned int adc_data_H; // ***************************** // 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 = 0b11101011; // ***************************** // 2.Program main // ***************************** while(1){ adc_data = Get_adc(); adc_data_H = ( adc_data / 85 ) + 9; adc_data_L = 200 - adc_data_H; PORTAbits.RA2 = 1; Wait_01nmS(adc_data_H); PORTAbits.RA2 = 0; Wait_01nmS(adc_data_L); }; } //****************************************************************************************** // Function Name : Wait_001nmS // Title : This function will wait n times of 1 mS // Input : // Output : //****************************************************************************************** void Wait_01nmS(unsigned int n) { unsigned int lp; for(lp=0;lp<( n );lp++){ Wait_01mS(); } } //****************************************************************************************** // Function Name : Wait_01mS // Title : This function will wait 0.1mS // Input : // Output : //****************************************************************************************** void Wait_01mS(void) { unsigned int lp; for(lp=0;lp<23;lp++){ asm("nop"); } } //****************************************************************************************** // 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 // BSF ADCON0,GO_NOT_DONE ADCON0 |= 0b00000010; // ***************** // 2.Get data // ***************** // (1)Wait for the adc start flag // BTFSC ADCON0,GO_NOT_DONE // GOTO $-1 while((ADCON0 & 0b00000010) != 0); // (2)Substitute the result // MOVF ADRESH,W // MOVWF ADDAT_H // MOVF ADRESL,W // MOVWF ADDAT_L adc_data = ( ADRESH << 8 ) + ADRESL ; return(adc_data); }