Here is the data_write function I'm looking at. I've been told that this method (from the textbook) is not good and I should be using the C method (that is currently commented out). When the Cbits method is used, any arguments after the _asm/_endasm the compiler doesn't recognize the bit names (they don't turn green).
If I use only the assembly method, I get a string of errors [1111] about the undefined label (WR, GIE, EECON1, etc.) inside this function. Do I have to somehow re-define the ADC.h inside the function? Basically, all of the register names (EECON1, etc) and bit names (WR, WREN, etc.) are not recognized inside the function "data_write" if they are inside the assembly tags (_asm/_endasm). If I use the Cbits.BIT method, I get a generic syntax error and the bits following _endasm are not identified (turn green).
#include <stdio.h>
#include <stdlib.h>
#include <p18f452.h>
#include <delays.h>
#include <adc.h>
int result; // used in ADC result handling
int write_point=0x64; // used in data_write function. initial=d100
void data_write (int write_point, int result); // declare prototype
void main(void) // <==should this be 'int main (void)' b/c i'm passing result to another function ?
{
while (1) {
// sensor 1 configured to port AN0
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_5ANA_0REF, ADC_CH0 & ADC_INT_OFF);
//configures ADC for port AN0 = sensor 1 input
ConvertADC(); // initiate conversion of sensor1 @ AN0
while(BusyADC()); // waiting to complete conversion
result=ReadADC(); // read the result of sensor1 @ AN0
data_write(result, write_point);
CloseADC();
}
}
void data_write (int write_point, int result) {
/* EECON1bits.EEPGD = 0 // points to data memory
EECON1bits.CFGS = 0 // access data eeprom
EECON1bits.WREN = 1 // enable write to data eeprom
INTCONbits.GIE=0 // disable interrupt
** if I substitute this method of bit control, this 2nd portion belongs after
* the asm arguments, but the bits (WR, GIE, WREN) will not be identified
* either an I get a syntax error. (of course commenting out the bcf/bsf statements)
EECON1bits.WR = 1 // enable bit to start the write operation
INTCONbits.GIE = 1 // re-enable interrupt
EECON1bits.WREN = 0 // restores the write command to =disabled */
_asm
movlw write_point // starting data memory address = data_adr
movwf EEADR,A
movlw result // gets data stored in "result" variable
movwf EEDATA,A // places data into data memory holder
bcf EECON1,EEPGD,A // points to data memory
bcf EECON1,CFGS,A // access data eeprom
bsf EECON1,WREN,A // enable write to data EEPROM
bcf INTCON,GIE,A // disable interrupt
movlw 0x55 // start flash erase sequence
movwf EECON2,A
movlw 0xAA
movwf EECON2,A // end flash erase sequence
bsf EECONN1,WR,A // enable bit to start the write operation
bsf INTCON,GIE,A // re-enable interrupt
bcf EECON1,WREN // restores the write command to =disabled
_endasm
write_point = write_point+2; // increment address by 2
if (write_point >= 0xC6) // if address >= d'198
write_point = 0x64; // resets starting point to d'100
}
Best Answer
For the commented out C part, you don't appear to have have any ending semicolons (e.g. first line should be
EECON1bits.EEPGD = 0;
) , so the compiler certainly won't like that.For the inline assembly, IIRC the register names hould b e usable, but I think you have to specify operands fully (e.g. instead of
movwf EECON2,A
you domovwf EECON2,0
)It's possible the bit names must be specified by number instead of name too, for example instead of
bcf EECON1,EEPGD,A
you dobcf EECON1, 7, 0
(EEPGD is bit 7 of EECON1) TheA
at the end is not right anyway, this should be 1 or 0 (or W or F in MPASM)It's been a while since I used the inline assembly for anything like this so memory is fuzzy, but try it and let us know how it goes.