writing to eeprom

Post your requests and ideas for the mikroElektronika website & forums.
Post Reply
Author
Message
Iversonnair
Posts: 1
Joined: 10 Jul 2014 07:11

writing to eeprom

#1 Post by Iversonnair » 10 Jul 2014 07:35

Hi All
im new to programming and having problems writing to eeprom. The code below is to set freq. value when a push button is pressed and then each time the freq. value is greater than the set value the selected port goes high. The program works fine but each time i total power away from the pic 16f887 it looses the setting can anyone please assist.
Thanking you in advance
Ivan
//* Copyright:
//IVAN NAIR
//* Description:
//The incoming frequency to be measured must be
//connected to PORTC. (T1CKI) pin.
//* Test configuration:
//MCU: PIC16F887
//Oscillator: HS, 20.0000 MHz
// out put relay connected to port D2

//******************************************************************************/

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
#include"BUILT_IN.H"
EEPROM_WRITE(set_freq);
EEPROM_READ(set_freq);
unsigned char int_sec_count; // used to count ints/second
unsigned char new_second; // is set once per second
unsigned char tchar; // text char used in LCD display numbers
unsigned int freq; // 0-65000, holds freq value to display integer hertz
unsigned int set_freq; // set freq when but is pressed
unsigned long rpm; // holds RPM for calcs AND display RPM= Revolution pr minute
char txt[12]; // used to display number string
char sec_count;

//-----------------------------------------------------------------------------
void interrupt()
{
// this is TMR2 overflow interrupt
int_sec_count--;
if(!int_sec_count) // if reached 1 second!
{
// get the TMR1 count!
T1CON = 0; // TMR1 OFF
freq = ((TMR1H << 8) + (TMR1L)); // put TMR1 16bit value in freq 
TMR1L = 0; // clear TMR1
TMR1H = 0;
T1CON = 0b00000011; // TMR1 back ON again

// that's everything done for this second
new_second++;
int_sec_count = 125; // load ready to generate another second
}
TMR2IF_bit = 0; // Clear TMR2IF before exit
}

//-----------------------------------------------------------------------------//
void main()
{

//-------------------------------------------------------
// setup PIC 16F887 registers 
ANSEL = 0; // Configure AN pins as digital
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

TRISC = 0b00000001; // PORTC.F0 = input from freq signal
PORTA = 0b11111111;
TRISA = 0b11111111;
TRISB = 0; //
PORTB = 0xFF; //
TRISB = 0xff; //
PORTD = 0b00000000;
TRISD = 0b00000000;
EEPROM_READ;
//-------------------------------------------------------


// show startup message
//Lcd_Config(0); // Initialize Lcd over SPI interface
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,2, "FREQUENCY"); // display startup text to Lcd
Lcd_Out(2,2, "SPEED");
Delay_1sec();
Delay_1sec();

// clear LCD again before main

Lcd_Cmd(_LCD_CLEAR);

//-------------------------------------------------------
// setup the timers for frequency counting

// setup TMR1
T1CON = 0b00000011; // TMR1 ON, external clock pulse on PORTC.F0

// setup TMR2 and enable TMR2 int
T2CON = 0b00011111; // TMR2 ON, 8MHz xtal, 16:4:1 = 31250 Hz (Timer 2 prescaler settings)
PR2 = 250; // TMR2 int is 31250 / 250 = 125 ints/sec
PIE1.TMR2IE = 1; // TMR2 interrupt is on
INTCON = 0b11000000; // GIE=ON, PIE=ON

// load variables ready to run
int_sec_count = 125;
//-------------------------------------------------------
// now do the main run loop
while(1)
{
// safe limit freq at 65 kHz
if(freq > 65000) freq =65000;

// everytime we reach a second, calculate and display freq
if(new_second)
{
new_second = 0;
freq = freq;


// display freq as "xxxxx Hz"
WordToStr(freq,txt);
Lcd_Out(1,8,txt);
Lcd_Out(1,14,"Hz");

// calc RPM from freq
rpm = (freq / 1.4); // for speed divide by 1.4* for rpm * 3.4 *****************

// format rpm to display as "xxxxxxx RPM"
LongToStr(rpm,txt); // get the rpm as a string
Lcd_Out(2,2,txt); // and display RPM!
Lcd_Out(2,14,"SPEED");

// format to lock speed & rpms

if (Button(&portA,2,0,1))
set_freq = freq;
EEPROM_WRITE(Lo,set_freq);
EEPROM_WRITE(Hi,set_freq);
EEPROM_READ(freq>set_freq)

(PORTD.RD2 = 1);
if(freq<set_freq)
(PORTD.RD2 = 0);

}
}
}

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: writing to eeprom

#2 Post by Muphy » 11 Sep 2014 15:18

This code cannot compile on it's own, you must have more code somewhere as variables such as Lo and Hi are not defined and the lines setting the registers do not work

I do not understand why you have the EEPROM_WRITE and EEPROM_READ directly below the include for BUILT_IN.H.

What do you think you intended with this line: EEPROM_READ(freq>set_freq)

What do you think this line does: freq = freq;

A bit of code formatting might also help, just a personal thing :-)

Just a couple of things to think about.

Mark

Post Reply

Return to “Website & Forums Wishlist”