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 support@ccsinfo.com

LED I/O Update Problem w/ Shadow Register

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



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

LED I/O Update Problem w/ Shadow Register
PostPosted: Thu May 12, 2005 12:02 am     Reply with quote

I tried to follow PCM programmer's code for creating a shadow register to change the output values of each pin on portA using a shadow register, but I must have gone wrong somewhere because none of the LEDs I have connected to port A light up after using this method. Maybe it is because the example given was using a PIC16 and I am using a PIC18F1320 so portA is located in a different spot? I have never been able to figure out how people know what hex value to assign a port when defining it for writing. Example: #byte PORT_A = 0x05. How do you know that portA is located there? Here is my code:

Code:

#include <18F1320.h> //Define chip as PIC18F320
#fuses NOWDT,NOPROTECT,NOLVP,NOMCLR,INTRC_IO// CCPB0
#use delay(clock=8000000) //Set clock to 8MHz
#byte PORT_A        = 0x05

int8 Shadow_Port_A = 0;

#bit  LED0          = Shadow_Port_A.0
#bit  LED1          = Shadow_Port_A.1
#bit  LED2          = Shadow_Port_A.2
#bit  LED3          = Shadow_Port_A.3
#bit  LED4          = Shadow_Port_A.4
#bit  LED5          = Shadow_Port_A.5
#bit  LED6          = Shadow_Port_A.6
#bit  LED7          = Shadow_Port_A.7

// This macro updates Port A bits via a shadow register.
#define update_Port_A(bit ,value)  bit = value; PORT_A = Shadow_Port_A

void g()
{
   update_Port_A(LED0, 0); //Column 1
   update_Port_A(LED1, 0);
   update_Port_A(LED2, 1);
   update_Port_A(LED3, 1);
   update_Port_A(LED4, 1);
   update_Port_A(LED5, 1);
   update_Port_A(LED6, 1);
   update_Port_A(LED7, 0);
   delay_ms(5);
   update_Port_A(LED0, 0); //Column 2
   update_Port_A(LED1, 1);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 0);
   update_Port_A(LED5, 0);
   update_Port_A(LED6, 0);
   update_Port_A(LED7, 1);
   delay_ms(5);
   update_Port_A(LED0, 1); //Column 3
   update_Port_A(LED1, 0);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 0);
   update_Port_A(LED5, 0);
   update_Port_A(LED6, 0);
   update_Port_A(LED7, 1);
   delay_ms(5);
   update_Port_A(LED0, 1); //Column 4
   update_Port_A(LED1, 0);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 0);
   update_Port_A(LED5, 0);
   update_Port_A(LED6, 0);
   update_Port_A(LED7, 1);
   delay_ms(5);
   update_Port_A(LED0, 1); //Column 5
   update_Port_A(LED1, 0);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 1);
   update_Port_A(LED5, 0);
   update_Port_A(LED6, 0);
   update_Port_A(LED7, 1);
   delay_ms(5);
   update_Port_A(LED0, 1); //Column 6
   update_Port_A(LED1, 0);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 1);
   update_Port_A(LED5, 0);
   update_Port_A(LED6, 0);
   update_Port_A(LED7, 1);
   delay_ms(5);
   update_Port_A(LED0, 0); //Column 7
   update_Port_A(LED1, 1);
   update_Port_A(LED2, 0);
   update_Port_A(LED3, 0);
   update_Port_A(LED4, 1);
   update_Port_A(LED5, 1);
   update_Port_A(LED6, 1);
   update_Port_A(LED7, 1);
   delay_ms(5);
}

void main()
{

   output_a(0x00);
   setup_adc(no_analogs);

   while(1)
   {
      g(); //Displays G in a loop at 200Hz
   }
}


Any guidance would be appreciated!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 12, 2005 12:41 am     Reply with quote

To find the address of a register:

1. Dowload the data sheet from the Microchip website and load it
into Acrobat Reader.

2. Look in the Table of Contents on the left, and open the section
called Memory Organization.

3. From there, go to Data Memory Organization.

4. Finally, click on Special Function Registers in the table of contents.

5. Now on the right, you should see a table called Special Function
Register Map (or sometimes they call it a Summary).
This section shows all the registers and their addresses.
The address of the PORTA register is in the lower right corner.

Actually, because you're using an 18F series PIC, it has Latch registers.
These were specifically added to get rid of the RMW problem. Read
the data sheet in the I/O Ports section. It explains this.
You don't actually need to maintain a shadow register. Just do your
operations directly on the LATA register.
Bryan



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

PostPosted: Thu May 12, 2005 12:58 am     Reply with quote

For some reason, all LEDs except those connected to pins A4 and A5 work fine. The LEDs connected to pins A4 and A5 never turn on, and according to the code they should. Looking at the specs, there are no analogs connected to those ports so I am confused as to why they never come on. Here is the code:

Code:

#include <18F1320.h> //Define chip as PIC18F320
#fuses NOWDT,NOPROTECT,NOLVP,NOMCLR,INTRC_IO// CCPB0
#use delay(clock=8000000) //Set clock to 8MHz

void g()
{
   output_a(0b10111110);
   delay_ms(5);
   output_a(0b01000001);
   delay_ms(5);
   output_a(0b10000001);
   delay_ms(5);
   output_a(0b10000001);
   delay_ms(5);
   output_a(0b10001001);
   delay_ms(5);
   output_a(0b10001001);
   delay_ms(5);
   output_a(0b01001111);
   delay_ms(5);
}

void main()
{

   output_a(0x00);
   setup_adc(no_analogs);

   while(1)
   {
      g(); //Displays G in a loop at 200Hz
   }
}


Any ideas?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 12, 2005 1:21 am     Reply with quote

Yes, but I'm going to make you go to the data sheet and look it up.
Go to Table 10-1: PORTA Functions. Look at the descriptions
for RA4 and RA5. When they are configured as i/o pins, they
are not as fully functional as other i/o pins. The chart will
certainly make it clear what's wrong with RA5.

To find out how to fix the RA4 problem, go to the board's search page,
and search for this: RA4 LED
Set it to "Search for all terms". There are several articles on the topic.
Bryan



Joined: 23 Apr 2005
Posts: 73

View user's profile Send private message Send e-mail AIM Address

PostPosted: Thu May 12, 2005 11:50 am     Reply with quote

Ok, thanks for the help! I read the specs and it looks like no matter what (whether MCLR is on or off) you can only use RA5 as an input, meaning no outputs and thus no LED. I got RA4 working by increasing it's voltage via a pullup resistor to Vdd as was suggested in one of the other posts. Do you know of any way around the problem of pin RA5 only being an input or am I going to have to a) buy another PIC with more pins or b) use a portb pin?
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