|
|
View previous topic :: View next topic |
Author |
Message |
jab351c
Joined: 27 Nov 2006 Posts: 9
|
reading a port as a byte |
Posted: Sun Dec 03, 2006 9:58 pm |
|
|
Ok, I have set port A as an input. Now I am trying to display the decimal value of the port on an LCD. Can someone please tell me what I am doing wrong? I believe my main problem is that I do not know how to read the port.
thanks,
Jeff
Code: | #include <18F4620.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOMCLR,BROWNOUT,PUT
#use delay(clock=10000000)
#use I2C(master, SCL=PIN_C3, SDA=PIN_C4)
#include <Flex_LCD420.c>
#define porta int(b1)
float b1;
////////////////////////////////////////////////////
void main()
{
setup_adc(ADC_OFF);
set_tris_a(0b11111111);
lcd_init();
lcd_putc(0x0c);
lcd_gotoxy(1,1);
printf(lcd_putc,"=%lf",b1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 03, 2006 10:13 pm |
|
|
Quote: | I believe my main problem is that I do not know how to read the port |
Download the CCS manual.
http://www.ccsinfo.com/downloads.php
Look in the table of Built-in Functions, in the section on Discrete I/O,
on page 116 (Page 126 in the Acrobat reader). It has a list of i/o
functions that will operation on an entire port, or on pins. Which
one looks like it will do input from port A ?
Also, if you read the value of port A, it will be an unsigned int.
It won't be a floating point value. |
|
|
jab351c
Joined: 27 Nov 2006 Posts: 9
|
|
Posted: Sun Dec 03, 2006 11:28 pm |
|
|
I think I am getting closer, atleast something is displaying. The only problem is that the LCD is displaying "=8". I believe that this is incorrect. The state of port A is 11001000 from pinA_7 to pin_A0 respectively. This is assuming that pin 6 & 7 are at logic high because that is where my oscillator ckt is. But after doing a test just right now in the middle of writing this I proved myself wrong. Pins 6 & 7 must be low. But how is this if I measure a voltage of approximately 200mV at each of these pins? I thought that logic high was any voltage but exactly 0 volts. Well anyways my code works now but an explanation of voltages for logic high/low would be helpful.
thanks,
Jeff
Code: | #include <18F4620.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOMCLR,BROWNOUT,PUT
#use delay(clock=10000000) #use I2C(master, SCL=PIN_C3, SDA=PIN_C4)
#include <Flex_LCD420.c>
unsigned int value;
////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
setup_adc(ADC_OFF);
set_tris_a(0b11111111);
value = input_a();
lcd_init();
lcd_putc(0x0c);
lcd_gotoxy(1,1);
printf(lcd_putc,"=%u",value);
} |
|
|
|
MarcosAmbrose
Joined: 25 Sep 2006 Posts: 38 Location: Adelaide, Australia
|
Re: reading a port as a byte |
Posted: Sun Dec 03, 2006 11:40 pm |
|
|
Hi there,
Put a loop at the end of your program (see code below). Not sure if this is the cause of the problem, but it certainly not helping
[quote= Code: |
void main()
{
setup_adc(ADC_OFF);
set_tris_a(0b11111111);
lcd_init();
lcd_putc(0x0c);
lcd_gotoxy(1,1);
printf(lcd_putc,"=%lf",b1);
//loop here forever
while(1)
{}
} | [/quote] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Dec 04, 2006 12:13 am |
|
|
Quote: | I thought that logic high was any voltage but exactly 0 volts |
Download the 18F4620 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39626b.pdf
Look at Table 26.3 on DC Characteristics (page 338 in the Acrobat reader).
The Vih value defines a 'logic high'. For TTL inputs, it's 2.0 volts.
For schmitt trigger inputs, it's 4.0v (if the PIC is running at 5v).
However, you're using pins A6 and A7 for the oscillator. Look in
section 10.1 in the data sheet, in the I/O port section. It says:
Quote: |
Pins RA6 and RA7 are multiplexed with the main oscillator pins;
they are enabled as oscillator or I/O pins by the selection of
the main oscillator in the configuration register (see Section
23.1 “Configuration Bits” for details). When they are not used
as port pins, RA6 and RA7 and their associated TRIS and LAT
bits are read as ‘0’. |
So you should be reading those pins as 0's. But it doesn't really
matter what their value is. Because you're not using those pins,
you're not interested in it. It's best to mask them off (set them
to 0). You can do this by AND'ing the value read with 0x3F.
Example:
Code: | value = input_a() & 0x3F; |
|
|
|
|
|
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
|