PDA

View Full Version : Reading analogs with 16F877



Andre_Pretorius
- 7th July 2008, 17:48
I am trying to read 4 Analog inputs with a pic and deliver them out on a hardware serial port. For some reason nothing happens. I am new to the 16F877 any help would be apreciated.

INCLUDE "MODEDEFS.BAS"
INCLUDE "ANSI.INC"

DEFINE OSC 20 ' Define crystal as 20Mhz

'*Serial port Setup 9600 8N1*
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically

'Define ACDIN parameters
Define ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source ( 3 = RC )
DEFINE ADC_SAMPLEUS 50 ' Set sampling time to micro-seconds

'This Part set PORTA 0-5 an analog inputs
TRISA = %00011111 'set PORTA 0-5 as inputs
ADCON1 = %10000000 'Right justify output of ADC use vss and vdd as ref.
ADCON0 = %11100000 'FRC (clock derived from a dedicated internal oscillator)

TRISC = %10000000 'RC7 as RX(input) rest as output

V1 var WORD
V2 var WORD
V3 var WORD
V4 var WORD

Main:
@ ClearScr ; Clear Screen
ADCIN 0,v1
ADCIN 1,v2
ADCIN 2,v3
ADCIN 3,v4
HSEROUT [DEC v1,13,10]
HSEROUT [DEC v2,13,10]
HSEROUT [DEC v3,13,10]
HSEROUT [DEC v4,13,10]
goto main

tenaja
- 7th July 2008, 18:16
You also have to configure your usart. You are missing the txsta and rxsta settings; read the datasheet and make sure you have all of the settings necessary. For instance, txsta.5 enables transmit...

Andre_Pretorius
- 7th July 2008, 18:26
Thanks for the info so far i added the folowing two lines but still no joy
TXSTA = %10100100
RCSTA = %10010000

Archangel
- 7th July 2008, 22:01
I am trying to read 4 Analog inputs with a pic and deliver them out on a hardware serial port. For some reason nothing happens. I am new to the 16F877 any help would be apreciated.

INCLUDE "MODEDEFS.BAS"
INCLUDE "ANSI.INC"

DEFINE OSC 20 ' Define crystal as 20Mhz

'*Serial port Setup 9600 8N1*
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically

'Define ACDIN parameters
Define ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source ( 3 = RC )
DEFINE ADC_SAMPLEUS 50 ' Set sampling time to micro-seconds

'This Part set PORTA 0-5 an analog inputs
TRISA = %00011111 'set PORTA 0-5 as inputs
ADCON1 = %10000000 'Right justify output of ADC use vss and vdd as ref.
ADCON0 = %11100000 'FRC (clock derived from a dedicated internal oscillator)

TRISC = %10000000 'RC7 as RX(input) rest as output

V1 var WORD
V2 var WORD
V3 var WORD
V4 var WORD

Main:
@ ClearScr ; Clear Screen
ADCIN 0,v1
ADCIN 1,v2
ADCIN 2,v3
ADCIN 3,v4
HSEROUT [DEC v1,13,10]
HSEROUT [DEC v2,13,10]
HSEROUT [DEC v3,13,10]
HSEROUT [DEC v4,13,10]
goto main
Try Add : DEFINE HSER_SPBRG 129
also do not see any config fuse settings, I am at work so have no access to data sheets, does this chip have ansel ?

Andre_Pretorius
- 8th July 2008, 04:41
This is what i have so far, Still nothing
No mention of ansel in the data sheet, any other idea i have replaced the chip also and tested with a ossiliscope to try and see if mabe the chips was faulty still nothing

'*Serial port Setup 9600 8N1*
TXSTA = %10100100
RCSTA = %10010000
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically
DEFINE HSER_SPBRG 129
TRISC = %10000000 'RC7 as RX(input) rest as output

Archangel
- 8th July 2008, 06:15
This is what i have so far, Still nothing
No mention of ansel in the data sheet, any other idea i have replaced the chip also and tested with a ossiliscope to try and see if mabe the chips was faulty still nothing

'*Serial port Setup 9600 8N1*
TXSTA = %10100100
RCSTA = %10010000
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically
DEFINE HSER_SPBRG 129
TRISC = %10000000 <font color=red>'RC7 as RX(input) rest as output</font color>
RC6 output, RC7 input edit: I gotta rub my eyes ! Been starin' at this "Confuser too long" Anyway you're using Herserout, so you should be using RC6, Yes? So what's on the other end? Is it working? Oh , just nosey, what's in the ansi include file? Must be something of yours, never seen that one . . .
Cut this from your code and see:
TXSTA = %10100100
RCSTA = %10010000

Andre_Pretorius
- 8th July 2008, 14:49
Current code still nothing

'*Serial port Setup 9600 8N1*
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically
DEFINE HSER_SPBRG 129

Archangel
- 8th July 2008, 17:39
Hi Andre,
OK I think I would go to square 1. Open a new window in MCS and put in only the basics, your hser defines, a byte variable, load a value into the variable and then an HSEROUT statement, also the config statements, and OSC define. see if that works, no includes, no AD enable code, in fact disable for this test, then after this works, start adding stuff back in.

Andre_Pretorius
- 8th July 2008, 17:44
Just a bit more info here is the inc file i am using, in answer to JOE s question the the device on the other side is my PC running hyper terminal

NOLIST
ifdef PM_USED
LIST
include 'M16F87x.INC' ; PM header
device pic16F877, hs_osc, wdt_on, pwrt_on, lvp_off, protect_off
XALL
NOLIST
else
LIST
LIST p = 16F877, r = dec, w = -302
INCLUDE "P16F877.INC" ; MPASM Header
__config _HS_OSC & _WDT_ON & _PWRTE_ON & _LVP_OFF & _CP_OFF
NOLIST
endif
LIST

Andre_Pretorius
- 8th July 2008, 18:25
thanks Joe s i did it but still nothing, I am now running a new PIC with LED's on the TX and RX lines just to elimenate any hardware problems here is the current code


DEFINE OSC 20 ' Define crystal as 20Mhz

'*Serial port Setup 9600 8N1*
DEFINE HSER_BAUD 9600 ; 9600 Baud
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ; Clear overflow automatically
DEFINE HSER_SPBRG 129

TRISC = %10000000 'RC7 as RX(input) rest as output

Main:
HSEROUT ["hallo"]
goto main
end

Andre_Pretorius
- 8th July 2008, 19:07
Thank you JOE S i found the problem, i went back to a led and a button when that did not wanted to work i went back to the hardware and found i used the wrong caps on the oscilator fixing it solved all the problems. again thank you for all the help