CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADC problem in pic c compiler v5.015

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eng.Nidal



Joined: 06 Jul 2016
Posts: 4

View user's profile Send private message

ADC problem in pic c compiler v5.015
PostPosted: Wed Jul 06, 2016 2:29 pm     Reply with quote

hi all

I'm always used to program my pic16f877a chip using pic c compiler v4.084 then a decided to install pic c compiler v5.015 which i found it easy to use especially it does not always need external library.


but
I have faced a problem in reading analogue signal. It does not read correctly, that happens if i compiled the code using v5.015 but if compiled the code using v4.084 the problem disappears.

i'm looking forward hopefully to find an answer for this problem because i'm not very expert on using ccs.

thanks a lot
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 06, 2016 4:28 pm     Reply with quote

You should post a small, compilable progam that fails. that way, we can 'cut/paste/compile/test' and quickly report back the results.
Also are you using MPLAB, if so version ??
Others will know if there's a 'bug'...

Jay
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Jul 06, 2016 9:06 pm     Reply with quote

5.015 is a VERY early v 5 release

buy the latest update. that ought to fix it too.
Eng.Nidal



Joined: 06 Jul 2016
Posts: 4

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 12:51 am     Reply with quote

this is the code its very simple as you can see
it works but not giving any right values
#include <main.h>

#define LCD_ENABLE_PIN PIN_B0
#define LCD_RS_PIN PIN_B1
#define LCD_RW_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6

#include <lcd.c>

void main()
{byte value;

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);

lcd_init();

while(TRUE)
{set_adc_channel(0);
value=read_adc();
output_c(value);
lcd_gotoxy(3,1);
printf(lcd_putc,"%u",value);


}

}code]
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 1:42 am     Reply with quote

Quote:
especially it does not always need external library.


V5, is _exactly the same as V4_, in regards to 'libraries'.
There is no such thing as a 'library' really in CCS.
You _need_ to be loading the include file(s) for the compiler version you are using, and the functions you want.

You very carefully do not show the file that loads these (main.h......).

Duh...

Now several comments:

1) ADC_CLOCK_INTERNAL, is almost certainly wrong _on every compiler version_. This is using the RC internal clock, _which the hardware does not recommend at clock rates above 1MHz_. Read the data sheet.
So, _wrong_....
It'll work, but give noisy ADC results. However since you seem to only be using the ADC as 8bit, this may well be acceptable.

2) You don't show us what ADC resolution you are selecting, but since you read the ADC into an 8bit value, hopefully the top of main.h, has:
Code:

#include <16F877a.h> //This needs to be including the V5 version
#device ADC=8 //Otherwise it is not going to give correct results....
//Then you don't show us your clock rate settings or fuses.....


3) 5.015!.... This is a very early V5 version. There is a fault in this version that it will disable global interrupts with printf. Since you are not using interrupts, probably won't matter, but this is really before a properly working version of V5. It's 'borderline'. Post V5.012, 90% of major stuff was running....

4) There is a major difference with V5, in that it leaves some fuses set as the chip defaults. V4, will instead set some fuses to 'probably working' values. Since you don't show us your fuses, this may well be why you are having problems.

5) The most likely problem is that you are using V4 include files with the V5 compiler.

So you need to show us the full program, not just 'your code'.
I've taken your posting, corrected the clock settings, and assumed a 10MHz crystal:
Code:

#include <16F877a.h> //This needs to be including the V5 version of this file
#device ADC=8 //8bit ADC operation
#fuses NOWDT, NOLVP, NODEBUG, BROWNOUT //Minimum fuses
#use delay(crystal=10MHz) //This is selecting HS

#define LCD_ENABLE_PIN PIN_B0
#define LCD_RS_PIN PIN_B1
#define LCD_RW_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6

#include <lcd.c>

void main(void)
{
   byte value;
   delay_ms(500); //allow time for the LCD to start
   setup_comparator(NC_NC_NC_NC); //ensure comparator is off
   setup_adc_ports(AN0); //AN0 only VSS to VDD as reference
   setup_adc(ADC_CLOCK_DIV_32); //correct clock setting for up to 20MHz

   lcd_init();
   set_adc_channel(0); //since only using one ADC channel select it here
   delay_us(10); //There must _always_ be at least Tacq between channel
   //select and read.....
   while(TRUE)
   {
       value=read_adc(); //now read value
       output_c(value); //output on port C
       lcd_gotoxy(3,1);
       printf(lcd_putc,"%u   ",value); //and display
       delay_ms(200); //LCD only supports about 4Hz refresh
   }
}


This merrily works, and gives a nice stable output. Added a couple of trailing spaces on the LCD output, otherwise if the value reduces, you get the old numbers still on the display, since there is no clear screen operation.
Eng.Nidal



Joined: 06 Jul 2016
Posts: 4

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 9:20 am     Reply with quote

I'm afraid to tell you that your code did not complete compiling. I don't know what i'm missing also in my program.
I'm still a beginner CCS programmer so some times i find some things are suspicious to me such as (fuses).

i really appreciate your help, i hope these are what you asked for from my program.

Code:

#include <16F877A.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(crystal=20000000)




Code:

               ROM used:   481 words (6%)
                           Largest free fragment is 2048
               RAM used:   7 (2%) at main() level
                           20 (5%) worst case
               Stack used: 5 locations
               Stack size: 8

*
0000:  MOVLW  01
0001:  MOVWF  0A
0002:  GOTO   1A7
0003:  NOP
.................... #include <main.h>
.................... #include <16F877A.h>
.................... //////////// Standard Header file for the PIC16F877A device ////////////////
.................... ///////////////////////////////////////////////////////////////////////////
.................... ////        (C) Copyright 1996, 2013 Custom Computer Services          ////
.................... //// This source code may only be used by licensed users of the CCS C  ////
.................... //// compiler.  This source code may only be distributed to other      ////
.................... //// licensed users of the CCS C compiler.  No other use, reproduction ////
.................... //// or distribution is permitted without written permission.          ////
.................... //// Derivative programs created using this software in object code    ////
.................... //// form are not restricted in any way.                               ////
.................... ///////////////////////////////////////////////////////////////////////////
.................... #device PIC16F877A
*
012F:  MOVF   25,W
0130:  CLRF   78
0131:  SUBWF  24,W
0132:  BTFSC  03.0
0133:  GOTO   137
0134:  MOVF   24,W
0135:  MOVWF  77
0136:  GOTO   143
0137:  CLRF   77
0138:  MOVLW  08
0139:  MOVWF  26
013A:  RLF    24,F
013B:  RLF    77,F
013C:  MOVF   25,W
013D:  SUBWF  77,W
013E:  BTFSC  03.0
013F:  MOVWF  77
0140:  RLF    78,F
0141:  DECFSZ 26,F
0142:  GOTO   13A
0143:  RETURN
*
0170:  MOVF   78,W
0171:  MOVF   22,W
0172:  MOVWF  24
0173:  MOVLW  64
0174:  MOVWF  25
0175:  CALL   12F
0176:  MOVF   77,W
0177:  MOVWF  22
0178:  MOVF   78,W
0179:  MOVLW  30
017A:  BTFSS  03.2
017B:  GOTO   183
017C:  BTFSS  23.1
017D:  GOTO   18A
017E:  BTFSC  23.3
017F:  GOTO   18A
0180:  BTFSC  23.4
0181:  MOVLW  20
0182:  GOTO   186
0183:  BCF    23.3
0184:  BCF    23.4
0185:  BSF    23.0
0186:  ADDWF  78,F
0187:  MOVF   78,W
0188:  MOVWF  24
0189:  CALL   144
018A:  MOVF   22,W
018B:  MOVWF  24
018C:  MOVLW  0A
018D:  MOVWF  25
018E:  CALL   12F
018F:  MOVF   77,W
0190:  MOVWF  22
0191:  MOVF   78,W
0192:  MOVLW  30
0193:  BTFSS  03.2
0194:  GOTO   19B
0195:  BTFSC  23.3
0196:  GOTO   19F
0197:  BTFSS  23.0
0198:  GOTO   19F
0199:  BTFSC  23.4
019A:  MOVLW  20
019B:  ADDWF  78,F
019C:  MOVF   78,W
019D:  MOVWF  24
019E:  CALL   144
019F:  MOVLW  30
01A0:  ADDWF  22,F
01A1:  MOVF   22,W
01A2:  MOVWF  24
01A3:  CALL   144
01A4:  BCF    0A.3
01A5:  BCF    0A.4
01A6:  GOTO   1DF (RETURN)
.................... 
.................... #list
.................... 
.................... #device ADC=16
.................... 
.................... #FUSES NOWDT                    //No Watch Dog Timer
.................... #FUSES NOBROWNOUT               //No brownout reset
.................... #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
.................... 
.................... #use delay(crystal=20000000)
*
0004:  MOVLW  27
0005:  MOVWF  04
0006:  BCF    03.7
0007:  MOVF   00,W
0008:  BTFSC  03.2
0009:  GOTO   017
000A:  MOVLW  06
000B:  MOVWF  78
000C:  CLRF   77
000D:  DECFSZ 77,F
000E:  GOTO   00D
000F:  DECFSZ 78,F
0010:  GOTO   00C
0011:  MOVLW  7B
0012:  MOVWF  77
0013:  DECFSZ 77,F
0014:  GOTO   013
0015:  DECFSZ 00,F
0016:  GOTO   00A
0017:  RETURN
.................... 
.................... 
.................... 
.................... #define LCD_ENABLE_PIN PIN_B0
.................... #define LCD_RS_PIN PIN_B1
.................... #define LCD_RW_PIN PIN_B2
.................... #define LCD_DATA4 PIN_B3
.................... #define LCD_DATA5 PIN_B4
.................... #define LCD_DATA6 PIN_B5
.................... #define LCD_DATA7 PIN_B6
.................... 
.................... #include <lcd.c>
.................... ///////////////////////////////////////////////////////////////////////////////
.................... ////                             LCD.C                                     ////
.................... ////                 Driver for common LCD modules                         ////
.................... ////                                                                       ////
.................... ////  lcd_init()   Must be called before any other function.               ////
.................... ////                                                                       ////
.................... ////  lcd_putc(c)  Will display c on the next position of the LCD.         ////
.................... ////                 \a  Set cursor position to upper left                 ////
.................... ////                 \f  Clear display, set cursor to upper left           ////
.................... ////                 \n  Go to start of second line                        ////
.................... ////                 \b  Move back one position                            ////
.................... ////              If LCD_EXTENDED_NEWLINE is defined, the \n character     ////
.................... ////              will erase all remanining characters on the current      ////
.................... ////              line, and move the cursor to the beginning of the next   ////
.................... ////              line.                                                    ////
.................... ////              If LCD_EXTENDED_NEWLINE is defined, the \r character     ////
.................... ////              will move the cursor to the start of the current         ////
.................... ////              line.                                                    ////
.................... ////                                                                       ////
.................... ////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)        ////
.................... ////                                                                       ////
.................... ////  lcd_getc(x,y)   Returns character at position x,y on LCD             ////
.................... ////                                                                       ////
.................... ////  lcd_cursor_on(int1 on)   Turn the cursor on (on=TRUE) or off         ////
.................... ////              (on=FALSE).                                              ////
.................... ////                                                                       ////
.................... ////  lcd_set_cgram_char(w, *p)   Write a custom character to the CGRAM.   ////
.................... ////                                                                       ////
.................... ////                                                                       ////
.................... ////  CONFIGURATION                                                        ////
.................... ////  The LCD can be configured in one of two ways: a.) port access or     ////
.................... ////  b.) pin access.  Port access requires the entire 7 bit interface     ////
.................... ////  connected to one GPIO port, and the data bits (D4:D7 of the LCD)     ////
.................... ////  connected to sequential pins on the GPIO.  Pin access                ////
.................... ////  has no requirements, all 7 bits of the control interface can         ////
.................... ////  can be connected to any GPIO using several ports.                    ////
.................... ////                                                                       ////
.................... ////  To use port access, #define LCD_DATA_PORT to the SFR location of     ////
.................... ////  of the GPIO port that holds the interface, -AND- edit LCD_PIN_MAP    ////
.................... ////  of this file to configure the pin order.  If you are using a         ////
.................... ////  baseline PIC (PCB), then LCD_OUTPUT_MAP and LCD_INPUT_MAP also must  ////
.................... ////  be defined.                                                          ////
.................... ////                                                                       ////
.................... ////  Example of port access:                                              ////
.................... ////     #define LCD_DATA_PORT getenv("SFR:PORTD")                         ////
.................... ////                                                                       ////
.................... ////  To use pin access, the following pins must be defined:               ////
.................... ////     LCD_ENABLE_PIN                                                    ////
.................... ////     LCD_RS_PIN                                                        ////
.................... ////     LCD_RW_PIN                                                        ////
.................... ////     LCD_DATA4                                                         ////
.................... ////     LCD_DATA5                                                         ////
.................... ////     LCD_DATA6                                                         ////
.................... ////     LCD_DATA7                                                         ////
.................... ////                                                                       ////
.................... ////  Example of pin access:                                               ////
.................... ////     #define LCD_ENABLE_PIN  PIN_E0                                    ////
.................... ////     #define LCD_RS_PIN      PIN_E1                                    ////
.................... ////     #define LCD_RW_PIN      PIN_E2                                    ////
.................... ////     #define LCD_DATA4       PIN_D4                                    ////
.................... ////     #define LCD_DATA5       PIN_D5                                    ////
.................... ////     #define LCD_DATA6       PIN_D6                                    ////
.................... ////     #define LCD_DATA7       PIN_D7                                    ////
.................... ////                                                                       ////
.................... ///////////////////////////////////////////////////////////////////////////////
.................... ////        (C) Copyright 1996,2010 Custom Computer Services           ////
.................... //// This source code may only be used by licensed users of the CCS C  ////
.................... //// compiler.  This source code may only be distributed to other      ////
.................... //// licensed users of the CCS C compiler.  No other use, reproduction ////
.................... //// or distribution is permitted without written permission.          ////
.................... //// Derivative programs created using this software in object code    ////
.................... //// form are not restricted in any way.                               ////
.................... ///////////////////////////////////////////////////////////////////////////
.................... 
.................... #ifndef __LCD_C__
.................... #define __LCD_C__
.................... 
.................... // define the pinout.
.................... // only required if port access is being used.
.................... typedef struct   
.................... {                            // This structure is overlayed
....................    int1 enable;           // on to an I/O port to gain
....................    int1 rs;               // access to the LCD pins.
....................    int1 rw;               // The bits are allocated from
....................    int1 unused;           // low order up.  ENABLE will
....................    unsigned int     data : 4;         // be LSB pin of that port.
....................   #if defined(__PCD__)       // The port used will be LCD_DATA_PORT.
....................    unsigned int    reserved: 8;
....................   #endif
.................... } LCD_PIN_MAP;
.................... 
.................... // this is to improve compatability with previous LCD drivers that accepted
.................... // a define labeled 'use_portb_lcd' that configured the LCD onto port B.
.................... #if ((defined(use_portb_lcd)) && (use_portb_lcd==TRUE))
....................  #define LCD_DATA_PORT getenv("SFR:PORTB")
.................... #endif
.................... 
.................... #if defined(__PCB__)
....................    // these definitions only need to be modified for baseline PICs.
....................    // all other PICs use LCD_PIN_MAP or individual LCD_xxx pin definitions.
.................... /*                                    EN, RS,   RW,   UNUSED,  DATA  */
....................  const LCD_PIN_MAP LCD_OUTPUT_MAP =  {0,  0,    0,    0,       0};
....................  const LCD_PIN_MAP LCD_INPUT_MAP =   {0,  0,    0,    0,       0xF};
.................... #endif
.................... 
.................... ////////////////////// END CONFIGURATION ///////////////////////////////////
.................... 
.................... #ifndef LCD_ENABLE_PIN
....................    #define lcd_output_enable(x) lcdlat.enable=x
....................    #define lcd_enable_tris()   lcdtris.enable=0
.................... #else
....................    #define lcd_output_enable(x) output_bit(LCD_ENABLE_PIN, x)
....................    #define lcd_enable_tris()  output_drive(LCD_ENABLE_PIN)
.................... #endif
.................... 
.................... #ifndef LCD_RS_PIN
....................    #define lcd_output_rs(x) lcdlat.rs=x
....................    #define lcd_rs_tris()   lcdtris.rs=0
.................... #else
....................    #define lcd_output_rs(x) output_bit(LCD_RS_PIN, x)
....................    #define lcd_rs_tris()  output_drive(LCD_RS_PIN)
.................... #endif
.................... 
.................... #ifndef LCD_RW_PIN
....................    #define lcd_output_rw(x) lcdlat.rw=x
....................    #define lcd_rw_tris()   lcdtris.rw=0
.................... #else
....................    #define lcd_output_rw(x) output_bit(LCD_RW_PIN, x)
....................    #define lcd_rw_tris()  output_drive(LCD_RW_PIN)
.................... #endif
.................... 
.................... // original version of this library incorrectly labeled LCD_DATA0 as LCD_DATA4,
.................... // LCD_DATA1 as LCD_DATA5, and so on.  this block of code makes the driver
.................... // compatible with any code written for the original library
.................... #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3) && !defined(LCD_DATA4) && !defined(LCD_DATA5) && !defined(LCD_DATA6) && !defined(LCD_DATA7))
....................    #define  LCD_DATA4    LCD_DATA0
....................    #define  LCD_DATA5    LCD_DATA1
....................    #define  LCD_DATA6    LCD_DATA2
....................    #define  LCD_DATA7    LCD_DATA3
.................... #endif
.................... 
.................... #ifndef LCD_DATA4
.................... #ifndef LCD_DATA_PORT
....................    #if defined(__PCB__)
....................       #define LCD_DATA_PORT      0x06     //portb
....................       #define set_tris_lcd(x)   set_tris_b(x)
....................    #else
....................      #if defined(PIN_D0)
....................       #define LCD_DATA_PORT      getenv("SFR:PORTD")     //portd
....................      #else
....................       #define LCD_DATA_PORT      getenv("SFR:PORTB")     //portb
....................      #endif
....................    #endif   
.................... #endif
.................... 
.................... #if defined(__PCB__)
....................    LCD_PIN_MAP lcd, lcdlat;
....................    #byte lcd = LCD_DATA_PORT
....................    #byte lcdlat = LCD_DATA_PORT
.................... #elif defined(__PCM__)
....................    LCD_PIN_MAP lcd, lcdlat, lcdtris;
....................    #byte lcd = LCD_DATA_PORT
....................    #byte lcdlat = LCD_DATA_PORT
....................    #byte lcdtris = LCD_DATA_PORT+0x80
.................... #elif defined(__PCH__)
....................    LCD_PIN_MAP lcd, lcdlat, lcdtris;
....................    #byte lcd = LCD_DATA_PORT
....................    #byte lcdlat = LCD_DATA_PORT+9
....................    #byte lcdtris = LCD_DATA_PORT+0x12
.................... #elif defined(__PCD__)
....................    LCD_PIN_MAP lcd, lcdlat, lcdtris;
....................    #word lcd = LCD_DATA_PORT
....................    #word lcdlat = LCD_DATA_PORT+2
....................    #word lcdtris = LCD_DATA_PORT-0x02
.................... #endif
.................... #endif   //LCD_DATA4 not defined
.................... 
.................... #ifndef LCD_TYPE
....................    #define LCD_TYPE 2           // 0=5x7, 1=5x10, 2=2 lines
.................... #endif
.................... 
.................... #ifndef LCD_LINE_TWO
....................    #define LCD_LINE_TWO 0x40    // LCD RAM address for the second line
.................... #endif
.................... 
.................... #ifndef LCD_LINE_LENGTH
....................    #define LCD_LINE_LENGTH 20
.................... #endif
.................... 
.................... unsigned int8 lcd_read_nibble(void);
.................... 
.................... unsigned int8 lcd_read_byte(void)
.................... {
....................    unsigned int8 low,high;
.................... 
....................  #if defined(__PCB__)
....................    set_tris_lcd(LCD_INPUT_MAP);
....................  #else
....................   #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
....................    output_float(LCD_DATA4);
*
0081:  BSF    06.3
....................    output_float(LCD_DATA5);
0082:  BSF    06.4
....................    output_float(LCD_DATA6);
0083:  BSF    06.5
....................    output_float(LCD_DATA7);
0084:  BSF    06.6
....................   #else
....................    lcdtris.data = 0xF;
....................   #endif
....................  #endif
....................         
....................    lcd_output_rw(1);
0085:  BCF    03.5
0086:  BSF    06.2
0087:  BSF    03.5
0088:  BCF    06.2
....................    delay_cycles(1);
0089:  NOP
....................    lcd_output_enable(1);
008A:  BCF    03.5
008B:  BSF    06.0
008C:  BSF    03.5
008D:  BCF    06.0
....................    delay_cycles(1);
008E:  NOP
....................    high = lcd_read_nibble();
008F:  BCF    03.5
0090:  CALL   048
0091:  MOVF   78,W
0092:  MOVWF  2C
....................       
....................    lcd_output_enable(0);
0093:  BCF    06.0
0094:  BSF    03.5
0095:  BCF    06.0
....................    delay_cycles(1);
0096:  NOP
....................    lcd_output_enable(1);
0097:  BCF    03.5
0098:  BSF    06.0
0099:  BSF    03.5
009A:  BCF    06.0
....................    delay_us(1);
009B:  GOTO   09C
009C:  GOTO   09D
009D:  NOP
....................    low = lcd_read_nibble();
009E:  BCF    03.5
009F:  CALL   048
00A0:  MOVF   78,W
00A1:  MOVWF  2B
....................       
....................    lcd_output_enable(0);
00A2:  BCF    06.0
00A3:  BSF    03.5
00A4:  BCF    06.0
.................... 
....................  #if defined(__PCB__)
....................    set_tris_lcd(LCD_OUTPUT_MAP);
....................  #else
....................   #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
....................    output_drive(LCD_DATA4);
00A5:  BCF    06.3
....................    output_drive(LCD_DATA5);
00A6:  BCF    06.4
....................    output_drive(LCD_DATA6);
00A7:  BCF    06.5
....................    output_drive(LCD_DATA7);
00A8:  BCF    06.6
....................   #else
....................    lcdtris.data = 0x0;
....................   #endif
....................  #endif
.................... 
....................    return( (high<<4) | low);
00A9:  BCF    03.5
00AA:  SWAPF  2C,W
00AB:  MOVWF  77
00AC:  MOVLW  F0
00AD:  ANDWF  77,F
00AE:  MOVF   77,W
00AF:  IORWF  2B,W
00B0:  MOVWF  78
.................... }
.................... 
.................... unsigned int8 lcd_read_nibble(void)
.................... {
....................   #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
*
0048:  CLRF   2D
....................    unsigned int8 n = 0x00;
.................... 
....................    /* Read the data port */
....................    n |= input(LCD_DATA4);
0049:  BSF    03.5
004A:  BSF    06.3
004B:  MOVLW  00
004C:  BCF    03.5
004D:  BTFSC  06.3
004E:  MOVLW  01
004F:  IORWF  2D,F
....................    n |= input(LCD_DATA5) << 1;
0050:  BSF    03.5
0051:  BSF    06.4
0052:  MOVLW  00
0053:  BCF    03.5
0054:  BTFSC  06.4
0055:  MOVLW  01
0056:  MOVWF  77
0057:  BCF    03.0
0058:  RLF    77,F
0059:  MOVF   77,W
005A:  IORWF  2D,F
....................    n |= input(LCD_DATA6) << 2;
005B:  BSF    03.5
005C:  BSF    06.5
005D:  MOVLW  00
005E:  BCF    03.5
005F:  BTFSC  06.5
0060:  MOVLW  01
0061:  MOVWF  77
0062:  RLF    77,F
0063:  RLF    77,F
0064:  MOVLW  FC
0065:  ANDWF  77,F
0066:  MOVF   77,W
0067:  IORWF  2D,F
....................    n |= input(LCD_DATA7) << 3;
0068:  BSF    03.5
0069:  BSF    06.6
006A:  MOVLW  00
006B:  BCF    03.5
006C:  BTFSC  06.6
006D:  MOVLW  01
006E:  MOVWF  77
006F:  RLF    77,F
0070:  RLF    77,F
0071:  RLF    77,F
0072:  MOVLW  F8
0073:  ANDWF  77,F
0074:  MOVF   77,W
0075:  IORWF  2D,F
....................     
....................    return(n);
0076:  MOVF   2D,W
0077:  MOVWF  78
....................   #else
0078:  RETURN
....................    return(lcd.data);
....................   #endif
.................... }
.................... 
.................... void lcd_send_nibble(unsigned int8 n)
.................... {
....................   #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
....................    /* Write to the data port */
....................    output_bit(LCD_DATA4, bit_test(n, 0));
*
0018:  BTFSC  2C.0
0019:  GOTO   01C
001A:  BCF    06.3
001B:  GOTO   01D
001C:  BSF    06.3
001D:  BSF    03.5
001E:  BCF    06.3
....................    output_bit(LCD_DATA5, bit_test(n, 1));
001F:  BCF    03.5
0020:  BTFSC  2C.1
0021:  GOTO   024
0022:  BCF    06.4
0023:  GOTO   025
0024:  BSF    06.4
0025:  BSF    03.5
0026:  BCF    06.4
....................    output_bit(LCD_DATA6, bit_test(n, 2));
0027:  BCF    03.5
0028:  BTFSC  2C.2
0029:  GOTO   02C
002A:  BCF    06.5
002B:  GOTO   02D
002C:  BSF    06.5
002D:  BSF    03.5
002E:  BCF    06.5
....................    output_bit(LCD_DATA7, bit_test(n, 3));
002F:  BCF    03.5
0030:  BTFSC  2C.3
0031:  GOTO   034
0032:  BCF    06.6
0033:  GOTO   035
0034:  BSF    06.6
0035:  BSF    03.5
0036:  BCF    06.6
....................   #else       
....................    lcdlat.data = n;
....................   #endif
....................       
....................    delay_cycles(1);
0037:  NOP
....................    lcd_output_enable(1);
0038:  BCF    03.5
0039:  BSF    06.0
003A:  BSF    03.5
003B:  BCF    06.0
....................    delay_us(2);
003C:  MOVLW  02
003D:  MOVWF  77
003E:  DECFSZ 77,F
003F:  GOTO   03E
0040:  GOTO   041
0041:  NOP
....................    lcd_output_enable(0);
0042:  BCF    03.5
0043:  BCF    06.0
0044:  BSF    03.5
0045:  BCF    06.0
0046:  BCF    03.5
0047:  RETURN
.................... }
.................... 
.................... void lcd_send_byte(unsigned int8 address, unsigned int8 n)
.................... {
....................   #if defined(__PCB__)
....................    set_tris_lcd(LCD_OUTPUT_MAP);
....................   #else
....................    lcd_enable_tris();
*
0079:  BSF    03.5
007A:  BCF    06.0
....................    lcd_rs_tris();
007B:  BCF    06.1
....................    lcd_rw_tris();
007C:  BCF    06.2
....................   #endif
.................... 
....................    lcd_output_rs(0);
007D:  BCF    03.5
007E:  BCF    06.1
007F:  BSF    03.5
0080:  BCF    06.1
....................    while ( bit_test(lcd_read_byte(),7) ) ;
*
00B1:  MOVF   78,W
00B2:  MOVWF  2B
00B3:  BTFSS  2B.7
00B4:  GOTO   0B7
00B5:  BSF    03.5
00B6:  GOTO   081
....................    lcd_output_rs(address);
00B7:  MOVF   29,F
00B8:  BTFSS  03.2
00B9:  GOTO   0BC
00BA:  BCF    06.1
00BB:  GOTO   0BD
00BC:  BSF    06.1
00BD:  BSF    03.5
00BE:  BCF    06.1
....................    delay_cycles(1);
00BF:  NOP
....................    lcd_output_rw(0);
00C0:  BCF    03.5
00C1:  BCF    06.2
00C2:  BSF    03.5
00C3:  BCF    06.2
....................    delay_cycles(1);
00C4:  NOP
....................    lcd_output_enable(0);
00C5:  BCF    03.5
00C6:  BCF    06.0
00C7:  BSF    03.5
00C8:  BCF    06.0
....................    lcd_send_nibble(n >> 4);
00C9:  BCF    03.5
00CA:  SWAPF  2A,W
00CB:  MOVWF  2B
00CC:  MOVLW  0F
00CD:  ANDWF  2B,F
00CE:  MOVF   2B,W
00CF:  MOVWF  2C
00D0:  CALL   018
....................    lcd_send_nibble(n & 0xf);
00D1:  MOVF   2A,W
00D2:  ANDLW  0F
00D3:  MOVWF  2B
00D4:  MOVWF  2C
00D5:  CALL   018
00D6:  RETURN
.................... }
.................... 
.................... #if defined(LCD_EXTENDED_NEWLINE)
.................... unsigned int8 g_LcdX, g_LcdY;
.................... #endif
.................... 
.................... void lcd_init(void) 
.................... {
....................    unsigned int8 i;
....................    unsigned int8 LCD_INIT_STRING[4] = {0x20 | (LCD_TYPE << 2), 0xc, 1, 6};
00D7:  MOVLW  28
00D8:  MOVWF  23
00D9:  MOVLW  0C
00DA:  MOVWF  24
00DB:  MOVLW  01
00DC:  MOVWF  25
00DD:  MOVLW  06
00DE:  MOVWF  26
....................                              // These bytes need to be sent to the LCD
....................                              // to start it up.
....................     
.................... 
....................    lcd_output_enable(0);
00DF:  BCF    06.0
00E0:  BSF    03.5
00E1:  BCF    06.0
....................    lcd_output_rs(0);
00E2:  BCF    03.5
00E3:  BCF    06.1
00E4:  BSF    03.5
00E5:  BCF    06.1
....................    lcd_output_rw(0);
00E6:  BCF    03.5
00E7:  BCF    06.2
00E8:  BSF    03.5
00E9:  BCF    06.2
.................... 
....................  #if defined(__PCB__)
....................    set_tris_lcd(LCD_OUTPUT_MAP);
....................  #else
....................   #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
....................    output_drive(LCD_DATA4);
00EA:  BCF    06.3
....................    output_drive(LCD_DATA5);
00EB:  BCF    06.4
....................    output_drive(LCD_DATA6);
00EC:  BCF    06.5
....................    output_drive(LCD_DATA7);
00ED:  BCF    06.6
....................   #else
....................    lcdtris.data = 0x0;
....................   #endif
....................    lcd_enable_tris();
00EE:  BCF    06.0
....................    lcd_rs_tris();
00EF:  BCF    06.1
....................    lcd_rw_tris();
00F0:  BCF    06.2
....................  #endif
....................     
....................    delay_ms(15);
00F1:  MOVLW  0F
00F2:  BCF    03.5
00F3:  MOVWF  27
00F4:  CALL   004
....................    for(i=1;i<=3;++i)
00F5:  MOVLW  01
00F6:  MOVWF  22
00F7:  MOVF   22,W
00F8:  SUBLW  03
00F9:  BTFSS  03.0
00FA:  GOTO   103
....................    {
....................        lcd_send_nibble(3);
00FB:  MOVLW  03
00FC:  MOVWF  2C
00FD:  CALL   018
....................        delay_ms(5);
00FE:  MOVLW  05
00FF:  MOVWF  27
0100:  CALL   004
0101:  INCF   22,F
0102:  GOTO   0F7
....................    }
....................     
....................    lcd_send_nibble(2);
0103:  MOVLW  02
0104:  MOVWF  2C
0105:  CALL   018
....................    delay_ms(5);
0106:  MOVLW  05
0107:  MOVWF  27
0108:  CALL   004
....................    for(i=0;i<=3;++i)
0109:  CLRF   22
010A:  MOVF   22,W
010B:  SUBLW  03
010C:  BTFSS  03.0
010D:  GOTO   11A
....................       lcd_send_byte(0,LCD_INIT_STRING[i]);
010E:  MOVLW  23
010F:  ADDWF  22,W
0110:  MOVWF  04
0111:  BCF    03.7
0112:  MOVF   00,W
0113:  MOVWF  27
0114:  CLRF   29
0115:  MOVF   27,W
0116:  MOVWF  2A
0117:  CALL   079
.................... 
....................   #if defined(LCD_EXTENDED_NEWLINE)
0118:  INCF   22,F
0119:  GOTO   10A
011A:  BCF    0A.3
011B:  BCF    0A.4
011C:  GOTO   1C1 (RETURN)
....................    g_LcdX = 0;
....................    g_LcdY = 0;
....................   #endif
.................... }
.................... 
.................... void lcd_gotoxy(unsigned int8 x, unsigned int8 y)
.................... {
....................    unsigned int8 address;
....................     
....................    if(y!=1)
011D:  DECFSZ 26,W
011E:  GOTO   120
011F:  GOTO   123
....................       address=LCD_LINE_TWO;
0120:  MOVLW  40
0121:  MOVWF  27
0122:  GOTO   124
....................    else
....................       address=0;
0123:  CLRF   27
....................       
....................    address+=x-1;
0124:  MOVLW  01
0125:  SUBWF  25,W
0126:  ADDWF  27,F
....................    lcd_send_byte(0,0x80|address);
0127:  MOVF   27,W
0128:  IORLW  80
0129:  MOVWF  28
012A:  CLRF   29
012B:  MOVF   28,W
012C:  MOVWF  2A
012D:  CALL   079
.................... 
....................   #if defined(LCD_EXTENDED_NEWLINE)
012E:  RETURN
....................    g_LcdX = x - 1;
....................    g_LcdY = y - 1;
....................   #endif
.................... }
.................... 
.................... void lcd_putc(char c)
.................... {
....................    switch (c)
*
0144:  MOVF   24,W
0145:  XORLW  07
0146:  BTFSC  03.2
0147:  GOTO   152
0148:  XORLW  0B
0149:  BTFSC  03.2
014A:  GOTO   157
014B:  XORLW  06
014C:  BTFSC  03.2
014D:  GOTO   15F
014E:  XORLW  02
014F:  BTFSC  03.2
0150:  GOTO   165
0151:  GOTO   16A
....................    {
....................       case '\a'   :  lcd_gotoxy(1,1);     break;
0152:  MOVLW  01
0153:  MOVWF  25
0154:  MOVWF  26
0155:  CALL   11D
0156:  GOTO   16F
.................... 
....................       case '\f'   :  lcd_send_byte(0,1);
0157:  CLRF   29
0158:  MOVLW  01
0159:  MOVWF  2A
015A:  CALL   079
....................                      delay_ms(2);
015B:  MOVLW  02
015C:  MOVWF  27
015D:  CALL   004
....................                     #if defined(LCD_EXTENDED_NEWLINE)
....................                      g_LcdX = 0;
....................                      g_LcdY = 0;
....................                     #endif
....................                      break;
015E:  GOTO   16F
.................... 
....................      #if defined(LCD_EXTENDED_NEWLINE)
....................       case '\r'   :  lcd_gotoxy(1, g_LcdY+1);   break;
....................       case '\n'   :
....................          while (g_LcdX++ < LCD_LINE_LENGTH)
....................          {
....................             lcd_send_byte(1, ' ');
....................          }
....................          lcd_gotoxy(1, g_LcdY+2);
....................          break;
....................      #else
....................       case '\n'   : lcd_gotoxy(1,2);        break;
015F:  MOVLW  01
0160:  MOVWF  25
0161:  MOVLW  02
0162:  MOVWF  26
0163:  CALL   11D
0164:  GOTO   16F
....................      #endif
....................       
....................       case '\b'   : lcd_send_byte(0,0x10);  break;
0165:  CLRF   29
0166:  MOVLW  10
0167:  MOVWF  2A
0168:  CALL   079
0169:  GOTO   16F
....................       
....................      #if defined(LCD_EXTENDED_NEWLINE)
....................       default     : 
....................          if (g_LcdX < LCD_LINE_LENGTH)
....................          {
....................             lcd_send_byte(1, c);
....................             g_LcdX++;
....................          }
....................          break;
....................      #else
....................       default     : lcd_send_byte(1,c);     break;
016A:  MOVLW  01
016B:  MOVWF  29
016C:  MOVF   24,W
016D:  MOVWF  2A
016E:  CALL   079
....................      #endif
....................    }
016F:  RETURN
.................... }
....................   
.................... char lcd_getc(unsigned int8 x, unsigned int8 y)
.................... {
....................    char value;
.................... 
....................    lcd_gotoxy(x,y);
....................    while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
....................    lcd_output_rs(1);
....................    value = lcd_read_byte();
....................    lcd_output_rs(0);
....................     
....................    return(value);
.................... }
.................... 
.................... // write a custom character to the ram
.................... // which is 0-7 and specifies which character array we are modifying.
.................... // ptr points to an array of 8 bytes, where each byte is the next row of
.................... //    pixels.  only bits 0-4 are used.  the last row is the cursor row, and
.................... //    usually you will want to leave this byte 0x00.
.................... void lcd_set_cgram_char(unsigned int8 which, unsigned int8 *ptr)
.................... {
....................    unsigned int i;
.................... 
....................    which <<= 3;
....................    which &= 0x38;
.................... 
....................    lcd_send_byte(0, 0x40 | which);  //set cgram address
.................... 
....................    for(i=0; i<8; i++)
....................    {
....................       lcd_send_byte(1, *ptr++);
....................    }
....................   
....................    #if defined(LCD_EXTENDED_NEWLINE)
....................     lcd_gotoxy(g_LcdX+1, g_LcdY+1);  //set ddram address
....................    #endif
.................... }
.................... 
.................... void lcd_cursor_on(int1 on)
.................... {
....................    if (on)
....................    {
....................       lcd_send_byte(0,0x0F);           //turn LCD cursor ON
....................    }
....................    else
....................    {
....................       lcd_send_byte(0,0x0C);           //turn LCD cursor OFF
....................    }
.................... }
.................... 
.................... #endif
.................... 
.................... 
.................... void main()
*
01A7:  MOVF   03,W
01A8:  ANDLW  1F
01A9:  MOVWF  03
01AA:  MOVLW  FF
01AB:  MOVWF  20
01AC:  BSF    03.5
01AD:  BSF    1F.0
01AE:  BSF    1F.1
01AF:  BSF    1F.2
01B0:  BCF    1F.3
01B1:  MOVLW  07
01B2:  MOVWF  1C
01B3:  BCF    03.7
.................... {byte value;
.................... 
....................    setup_adc_ports(AN0);
01B4:  BCF    1F.0
01B5:  BSF    1F.1
01B6:  BSF    1F.2
01B7:  BSF    1F.3
....................    setup_adc(ADC_CLOCK_internal);
01B8:  BCF    1F.6
01B9:  BCF    03.5
01BA:  BSF    1F.6
01BB:  BSF    1F.7
01BC:  BSF    03.5
01BD:  BCF    1F.7
01BE:  BCF    03.5
01BF:  BSF    1F.0
.................... 
....................    lcd_init();
01C0:  GOTO   0D7
.................... 
....................    while(TRUE)
....................    {set_adc_channel(0);
01C1:  MOVLW  00
01C2:  MOVWF  78
01C3:  MOVF   1F,W
01C4:  ANDLW  C7
01C5:  IORWF  78,W
01C6:  MOVWF  1F
....................    value=read_adc();
01C7:  BSF    1F.2
01C8:  BTFSC  1F.2
01C9:  GOTO   1C8
01CA:  BSF    03.5
01CB:  MOVF   1E,W
01CC:  BCF    03.5
01CD:  MOVWF  21
....................    output_c(value);
01CE:  MOVLW  00
01CF:  MOVWF  20
01D0:  BSF    03.5
01D1:  CLRF   07
01D2:  BCF    03.5
01D3:  MOVF   21,W
01D4:  MOVWF  07
....................    lcd_gotoxy(3,1);
01D5:  MOVLW  03
01D6:  MOVWF  25
01D7:  MOVLW  01
01D8:  MOVWF  26
01D9:  CALL   11D
....................    printf(lcd_putc,"%u",value);
01DA:  MOVF   21,W
01DB:  MOVWF  22
01DC:  MOVLW  1B
01DD:  MOVWF  23
01DE:  GOTO   170
01DF:  GOTO   1C1
....................   
....................       
....................    }
.................... 
.................... }
01E0:  SLEEP

Configuration Fuses:
   Word  1: 3F32   HS NOWDT PUT NOBROWNOUT NOLVP NOCPD NOWRT NODEBUG NOPROTECT
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 10:28 am     Reply with quote

this...
setup_adc(ADC_CLOCK_internal);

is still wrong !

As pointed out before..it needs to be changed..

to this...
setup_adc(ADC_CLOCK_DIV_32); //correct clock setting for up to 20MHz

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 11:28 am     Reply with quote

Also on the original code, he is reading the ADC into a single int8. He is now compiling with ADC=16.....

The code I posted compiles and runs with 5.015. It only needs the clock line changed to crystal=20MHz, to run with a 20MHz crystal.
Eng.Nidal



Joined: 06 Jul 2016
Posts: 4

View user's profile Send private message

PostPosted: Thu Jul 07, 2016 3:42 pm     Reply with quote

thanks a lot for helpful information

it's finally worked
and it reads perfect
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group