Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*---------------------------------------------/
- Name : Mohd Syahri Fikri Bin Samusi
- EEPROM with ADC
- Date Modified: 22 March 2012
- /---------------------------------------------*/
- #include <htc.h>
- #include <pic16f877a.h>
- #include <stdio.h>
- #define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
- #define neutral_zone 25 //0.5 Volts
- void init_rs232( )
- {
- BRGH = 1;
- SPBRG = 129; // set baud rate: Fosc=20Mhz, BR=9600
- TX9 = 0; // ninebits?1:0,,,8- or 9-bit transmission
- SYNC = 0; // asynchronous
- SPEN = 1; // enable serial port pins
- TXEN = 1; // enable the transmitter & (automatically)TXIF=1
- CREN = 1; // enable reception
- SREN = 0; // no effect
- TXIE = 0; // disable tx interrupts
- RCIE = 0; // disable rx interrupts
- }
- char read_eeprom (unsigned char add)
- {
- unsigned char a;
- EEADR=add; //adddress location
- EEPGD=0;//point eeprom
- RD=1;//to start rread operation
- while(RD==1);//wait to complete read operation
- a=EEDATA;//data is in EEDATA
- return a;
- }
- void write_eeprom(unsigned char add, unsigned char a)
- { //////EECON1reg=EEPGD — — — WRERR WREN WR RD/////
- EEADR=add;//adddress location
- EEDATA=a;//data
- EEPGD=0;//point eeprom
- WREN=1;//enable program operation
- //EEIF=0;
- EECON2=0x55;
- EECON2=0xAA;
- WR=1;
- WREN=0;//disable program operation
- while(EEIF==0);//wait to complete write
- EEIF=0;
- }
- void main( )
- {
- unsigned int cutoff=127, reading, reading1;
- init_rs232( );
- TRISA=0xFF; // Port A inputs
- TRISB=0xF0; // Port B0-B3 outputs
- TRISD=0x00;
- ADCON1=0b01001110; //Set ADFM (bit 7 of ADCON1) to 0 for left justified
- ADCON0=0b10000000;
- while (1)
- {
- __delay_ms(100);
- ADCON0=ADCON0|1;
- GO = 1; //Start A/D Conversion
- while(GO==1); //Wait end of conversion (conversion complete)
- cutoff = read_eeprom(100);
- reading = ADRESH; //Read the data
- if(reading<(cutoff-neutral_zone/2))
- //If analog input less than (2.5-0.5/2) which is 2.25V
- {
- RB0=0; //Turn on GREEN LED
- RB1=1;
- RB2=1;
- }
- else if (reading>(cutoff+neutral_zone/2))
- //If analog input more than (2.5+0.5/2) which is 2.75V
- {
- RB0=1;
- RB1=1;
- RB2=0; //Turn on RED LED
- }
- else
- {
- RB0=1;
- RB1=0; //Turn on YELLOW LED
- RB2=1;
- }
- if (!RA1) //The switch to Pin A1 is pressed
- {
- while(!RA1); //Wait for the switch to release
- __delay_ms(100);
- write_eeprom(100, reading); //Store ADC value into EEPROM
- printf("The cutoff value is %d \r\n", cutoff);
- PORTD=cutoff;
- }
- }
- } //End of Program
Advertisement
Add Comment
Please, Sign In to add comment