16F1786 issue with simple program.


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: 16F1786 issue with simple program.

    I figure a HelloWorld program and works fine in Proteus Simulator.
    But I stuck when I try to read AN0 analog port.
    The PicBasic ADCIN 0, something return "0".
    No better luck when I try to explicitly read AN0, like bellow:
    Code:
    'PIC16F1786
        #CONFIG
            __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON &  _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF
            __config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _VCAPEN_OFF
        #ENDCONFIG
        
        include "MODEDEFS.BAS"
        
        DEFINE OSC 8
        OSCCON.6 = 1
        OSCCON.5 = 1
        OSCCON.4 = 1
        OSCCON.3 = 0
        OSCCON.1 = 1
    
        TRISA = %00001001    'AN0 is Analog
        TRISB = %11000000
        TRISC = %00000000
    
        
        ANSELA = %00001001 'digital; AN0 is analog
        ANSELB = %00001001
    
        'Configure VREF:
    '    ADCON1.1 = 0
    '    ADCON1.0 = 1 'VREF+ is connected to VREF+ pin
        ADCON1.1 = 0
        ADCON1.0 = 0 'VREF+ is connected to VDD
    
        ADCON1.2 = 0 ' VREF- is connected to VSS
        
        'ADC result
        ADCON0.7 = 1 ' 10 bits result
        ADCON0.0 = 1 ' ADC is enabled   
        
        ADCON1.6 = 1 'FOSC/64
        ADCON1.5 = 1
        ADCON1.4 = 0
        
        CM1CON0.7 = 0'comparator is disabled
        CM2CON0.7 = 0'comparator is disabled
        
        DACCON0.7 = 0 'DAC is disabled
        
        'disable intrerupt:
        INTCON = 0'intrerupt is disabled
        
        DEFINE ADC_BITS 10       ' Set number of bits in result
        DEFINE ADC_CLOCK 3     ' Set clock source (rc = 3)
        DEFINE ADC_SAMPLEUS 50 ' Set sampling time in microseconds 
    
    
    '-[The connection between the LCD display and the microcontroller is as follows:]-
        ' Define LCD pins
        DEFINE LCD_DREG PORTC 'LCD data port 
        DEFINE LCD_DBIT 0 'LCD data starting bit 0 or 4 
        DEFINE LCD_RSREG PORTA 'LCD register select port 
        DEFINE LCD_RSBIT 2 'LCD register select bit       - pinul RS
        DEFINE LCD_EREG PORTA 'LCD enable port 
        DEFINE LCD_EBIT 7 'LCD enable bit                 - pinul E
    '    DEFINE LCD_RWREG PORTC 'LCD read/write port 
    '    DEFINE LCD_RWBIT 6 'LCD read/write bit 
        DEFINE LCD_BITS 4 'LCD bus size 4 or 8 
        DEFINE LCD_LINES 2 'Number lines on LCD 
        DEFINE LCD_COMMANDUS 2000 'Command delay time in us 
        DEFINE LCD_DATAUS 50 'Data delay time in us 
    
    ;-----[Variables]----------------------------------------------------
        Thermocouple  var word
        adval	      var	word    
    ;----[Initialization]-----------------------------------------------------------
    
    '---[Hello World]---------------------------------------------------------------
    Afisaj:
        LCDOUT $FE,1 ' Clear LCD
        LCDOUT $FE,2 ' Home cursor
        'Display initial screen
        lcdout $FE,2,"PIC16F1786",$fe, $C0,"Luxornet@2016"    
        pause 1000
        LCDOUT $FE,1 ' Clear LCD
        LCDOUT $FE,2 ' Home cursor
    
    ;----[Main program loop]--------------------------------------------------------
    Main:
        'ADCIN 0, Thermocouple
    
    '  	ADCON0 = %10000001	' Configure and turn on A/D Module
    
      	ADCON0.7 = 1 '10 bits result
    	pause 10
        ADCON0.2 = 0	' use channel 0
        ADCON0.3 = 0
        ADCON0.4 = 0
        ADCON0.5 = 0
        ADCON0.6 = 0
        
        ADCON0.0 = 1 'ADC is enabled 
        
        ADCON0.1 = 1    ' Start Conversion   'Bit GO/DONE  
    
    Voltmeter1:
        Pause 100
    	If ADCON0.1 = 1 Then Voltmeter1	' Wait for low on bit-1 of ADCON0, conversion finished
    
    	adval.highbyte = ADRESH	' Move HIGH byte of result to adval
    	adval.lowbyte = ADRESL	' Move LOW byte of result to adval
    
        lcdout $FE,2,"Thermocouple: ",dec adval,"   "
        pause 500
       
    GOTO Main
    Last edited by luxornet; - 24th January 2016 at 13:10.

Similar Threads

  1. 18F4550 issue with simple program.
    By jmgelba in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 20th January 2016, 11:48
  2. Program Configuration check mark issue... maybe.
    By Szczepaniec in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th November 2012, 18:17
  3. Simple LED driving issue - setup?
    By George in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th March 2011, 01:54
  4. Code issue with a flashing light(simple question)
    By IAmTheAnswer in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th May 2010, 11:46
  5. Help with simple program
    By mykewl in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 31st July 2008, 08:58

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts