Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 #include <p18f97j60.h> #include <stdio.h> #define XTAL_FREQ 25000000 #define ldata PORTE #define rs PORTHbits.RH2 #define rw PORTHbits.RH1 #define en PORTHbits.RH0 #define pot PORTAbits.RA2; int x,y,j; char buff[] = ""; void MSDelay(unsigned int itime){ unsigned int i,j; for(i=0;i<itime;i++) for(j=0;j<135;j++); } void lcdcmd(unsigned char value){ ldata = value; rs = 0; rw = 0; en = 1; MSDelay(1); en = 0; } void lcddata(unsigned char value){ ldata = value; rs = 1; rw = 0; en = 1; MSDelay(1); en = 0; } void lcdinit(void){ lcdcmd(0x01); //clear lcd screen MSDelay(20); lcdcmd(0x38); //lcd 2 lines, 5x7 matrix MSDelay(20); lcdcmd(0x0E); //display on, cursor on MSDelay(20); lcdcmd(0x01); //clear screen MSDelay(20); lcdcmd(0x06); //shift cursor right MSDelay(20); lcdcmd(0x80); //line 1, position 0 } void main(){ TRISA = 1; //port A for pot TRISE = 0; //port E for lcd data TRISH = 0; //port H for lcd control lines TRISJ = 0; //port J for led PORTJ = 0; en = 0; //enable pin 0 lcdinit(); //lcd initialization ADCON0 = 0b00001001; //ADC bits ADCON1 = 0b00101100; //.. ADCON2 = 0b00000010; //.. ADCON0bits.ADON = 1; PORTJbits.RJ0 = 1; //port J0 led on while(1){ PORTJbits.RJ0 = 0; PORTJbits.RJ2 = 0; //port J2 led off PORTJbits.RJ1 = 1; //port J1 led on ADCON0bits.GO = 1; //start ADC conversion lcddata('G'); //display G on lcd MSDelay(25); while(ADCON0bits.DONE == 1); //ADC conversion finish lcdcmd(0x01); MSDelay(10); x = ADRESH; x += (ADRESL<<8); itoa(x,buff); //convert int value from x to char and store in buff for(j=0;j<10;j++){ lcddata(buff[j]); //displaying buff } } }
For you it may look like garbage, but for us the display content could be a good information to find the error.lcd is showing some number and then garbage values
char buff[7];
.......
[URL="https://www.opengroup.org/onlinepubs/009695399/functions/itoa.html"][COLOR=#000066]itoa[/COLOR][/URL][COLOR=#009900]([/COLOR]x[COLOR=#339933],[/COLOR]buff[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]//convert int value from x to char and store in buff[/I][/COLOR] k=strlen(buff);
[COLOR=#B1B100]for[/COLOR][COLOR=#009900]([/COLOR]j[COLOR=#339933]=[/COLOR][COLOR=#0000DD]0[/COLOR][COLOR=#339933];[/COLOR]j[COLOR=#339933]<k;[/COLOR]j[COLOR=#339933]++[/COLOR][COLOR=#009900])
[/COLOR][COLOR=#009900] {[/COLOR]
lcddata[COLOR=#009900]([/COLOR]buff[COLOR=#009900][[/COLOR]j[COLOR=#009900]][/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]//displaying buff[/I][/COLOR]
[COLOR=#009900]}
[/COLOR]
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // CONFIG1L #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on SWDTEN bit)) #pragma config STVR = OFF // Stack Overflow/Underflow Reset Enable bit (Reset on stack overflow/underflow disabled) #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode)) Code: // CONFIG1H #pragma config CP0 = OFF // Code Protection bit (Program memory is not code-protected) // CONFIG2L #pragma config FOSC = ECPLL // Oscillator Selection bits (EC oscillator, PLL enabled and under software control, CLKO function on OSC2) #pragma config FOSC2 = ON // Default/Reset System Clock Select bit (Clock selected by FOSC1:FOSC0 as system clock is enabled when OSCCON<1:0> = 00) #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor enabled) #pragma config IESO = ON // Two-Speed Start-up (Internal/External Oscillator Switchover) Control bit (Two-Speed Start-up enabled) // CONFIG2H #pragma config WDTPS = 32768 // Watchdog Timer Postscaler Select bits (1:32768) // CONFIG3L #pragma config EASHFT = ON // External Address Bus Shift Enable bit (Address shifting enabled; address on external bus is offset to start at 000000h) #pragma config MODE = MM // External Memory Bus (Microcontroller mode, external bus disabled) #pragma config BW = 16 // Data Bus Width Select bit (16-Bit Data Width mode) #pragma config WAIT = OFF // External Bus Wait Enable bit (Wait states for operations on external memory bus disabled) // CONFIG3H #pragma config CCP2MX = ON // ECCP2 MUX bit (ECCP2/P2A is multiplexed with RC1) #pragma config ECCPMX = ON // ECCP MUX bit (ECCP1 outputs (P1B/P1C) are multiplexed with RE6 and RE5; ECCP3 outputs (P3B/P3C) are multiplexed with RE4 and RE3) #pragma config ETHLED = ON // Ethernet LED Enable bit (RA0/RA1 are multiplexed with LEDA/LEDB when Ethernet module is enabled and function as I/O when Ethernet is disabled)
char buff[] = "";
char buff[[COLOR="#FF0000"]25[/COLOR]];
char * buff = (char *) malloc(25*sizeof(char));
itoa(x,buff); //convert int value from x to char and store in buff
char buff[25]; //minima size must be 7 for itoa, but this buffer could be used for other purpose
unsigned int x;
ADCON2 = 0b10111110; //ADFM=1;u=0;ACQT210=111;ADSC2=110;
ADCON0bits.GO = 1; //start ADC conversion
lcddata('G'); //display G on lcd
Delay_ms(25);
while(ADCON0bits.DONE == 1); //ADC conversion finish
lcdcmd(0x01);
Delay_ms(10);
x = ADRESH <<8; // MSB
x =x + ADRESL; // LSB
itoa(x,buff); //convert int value from x to char and store in buff
k=strlen(buff);
for(j=0;j<k;j++)
{
lcddata(buff[j]); //displaying buff
}
... for that all I have to do is change the ADCON bits according to the channel being used, repeat steps for ADRESH and ADRESL registers to combine and display, right?
[
//ADCON0 = 0b00001001; //ADCAL=0;u=0;CHS3210=0010;GO=0;ADON=1;
ADCON0=( Chanel <<2)+1;
Delay_ms(20);
ADCON0bits.GO = 1; //start ADC conversion
Simple answer: The datasheet wil tell you.Is it necessary to give delay..????? Cause I don't want any delay when different analog values are displayed.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?