sp3ctrm5tr

PIC16F877A EEPROM

Apr 12th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. /*---------------------------------------------/
  2.     Name    : Mohd Syahri Fikri Bin Samusi
  3.    
  4.     EEPROM with ADC
  5.    
  6.     Date Modified: 22 March 2012
  7. /---------------------------------------------*/
  8.  
  9. #include <htc.h>
  10. #include <pic16f877a.h>
  11. #include <stdio.h>
  12. #define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
  13. #define neutral_zone 25 //0.5 Volts
  14.  
  15. void init_rs232( )
  16. {  
  17.     BRGH = 1;    
  18.     SPBRG = 129;    // set baud rate: Fosc=20Mhz, BR=9600
  19.     TX9  = 0;   // ninebits?1:0,,,8- or 9-bit transmission
  20.     SYNC = 0;   // asynchronous
  21.     SPEN = 1;   // enable serial port pins
  22.     TXEN = 1;   // enable the transmitter & (automatically)TXIF=1
  23.     CREN = 1;   // enable reception
  24.     SREN = 0;   // no effect
  25.     TXIE = 0;   // disable tx interrupts
  26.     RCIE = 0;   // disable rx interrupts
  27. }
  28.  
  29. char read_eeprom (unsigned char add)
  30. {  
  31.     unsigned char a;
  32.     EEADR=add; //adddress location
  33.     EEPGD=0;//point eeprom
  34.     RD=1;//to start rread operation
  35.     while(RD==1);//wait to complete read operation
  36.     a=EEDATA;//data is in EEDATA
  37.     return a;
  38. }
  39.  
  40. void write_eeprom(unsigned char add, unsigned char a)
  41. {   //////EECON1reg=EEPGD — — — WRERR WREN WR RD/////
  42.     EEADR=add;//adddress location
  43.     EEDATA=a;//data
  44.     EEPGD=0;//point eeprom
  45.     WREN=1;//enable program operation
  46.     //EEIF=0;
  47.     EECON2=0x55;
  48.     EECON2=0xAA;
  49.     WR=1;
  50.     WREN=0;//disable program operation
  51.     while(EEIF==0);//wait to complete write
  52.     EEIF=0;
  53. }
  54.  
  55. void main( )
  56. {  
  57.     unsigned int cutoff=127, reading, reading1;
  58.     init_rs232( );
  59.     TRISA=0xFF;    // Port A inputs
  60.     TRISB=0xF0;    // Port B0-B3 outputs
  61.     TRISD=0x00;    
  62.     ADCON1=0b01001110; //Set ADFM (bit 7 of ADCON1) to 0 for left justified
  63.     ADCON0=0b10000000;
  64.    
  65.  
  66.     while (1)
  67.     {  
  68.         __delay_ms(100);
  69.         ADCON0=ADCON0|1;
  70.         GO = 1; //Start A/D Conversion
  71.         while(GO==1); //Wait end of conversion (conversion complete)
  72.  
  73.         cutoff = read_eeprom(100); 
  74.         reading = ADRESH; //Read the data
  75.         if(reading<(cutoff-neutral_zone/2))
  76.         //If analog input less than (2.5-0.5/2) which is 2.25V
  77.         {  
  78.             RB0=0; //Turn on GREEN LED
  79.             RB1=1;
  80.             RB2=1;
  81.         }
  82.         else if (reading>(cutoff+neutral_zone/2))
  83.         //If analog input more than (2.5+0.5/2) which is 2.75V
  84.         {  
  85.             RB0=1;
  86.             RB1=1;
  87.             RB2=0; //Turn on RED LED
  88.         }
  89.         else
  90.         {  
  91.             RB0=1;
  92.             RB1=0; //Turn on YELLOW LED
  93.             RB2=1;
  94.         }
  95.  
  96.          if (!RA1) //The switch to Pin A1 is pressed
  97.         {  
  98.             while(!RA1); //Wait for the switch to release
  99.             __delay_ms(100);
  100.             write_eeprom(100, reading); //Store ADC value into EEPROM
  101.             printf("The cutoff value is %d \r\n", cutoff);
  102.             PORTD=cutoff;
  103.         }
  104.     }
  105. } //End of Program
Advertisement
Add Comment
Please, Sign In to add comment