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

Setting LATCH outputs with 8 bit binary
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

Setting LATCH outputs with 8 bit binary
PostPosted: Tue Jan 03, 2012 2:01 pm     Reply with quote

Hi
I'm trying to set the outputs of a Port with an 8 bit number, so that it could look like
Code:
PORTB = 11001001;

I'm using CCS compiler for the PIC18F4550.

I tried to use
Code:
LATB = 0b11001001;


but CCS came up with
Quote:
*** Error 12 "main.c" Line 131(6,10): Undefined identifier LATB


Thanks for your help.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 2:10 pm     Reply with quote

See Ttelmah's sample code in this post, which shows how to declare the
LATB identifier:
http://www.ccsinfo.com/forum/viewtopic.php?t=46997&start=6&highlight=byte+latb+getenv
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 03, 2012 3:21 pm     Reply with quote

Thanks for answering. I managed it to get no error, however no LED was turned on.
Code:
#byte LATB=getenv("SFR:LATB")
LATB = 0b11001001;

It didn't work with
Code:

output_b(0b11001001);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 3:45 pm     Reply with quote

Post a short little program that shows the problem. You should be able
to do this in 10 lines of code, including the #include, #fuses, #use delay()
at the start.

Also post your CCS compiler version. It's a 4-digit number given at
the top of the .LST file in your project directory.
temtronic



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

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 3:50 pm     Reply with quote

Be sure to have correct current limiting resistors with the LEDs, and they are wired properly ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19384

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 4:02 pm     Reply with quote

Some comments:
On PORTB of the 4550, several of the port pins are overridden by peripherals.
First, you need the NOLVP fuse (or B5, B6, and B7 will be the programming pins).
Then B3 is overridden by the PWM2 output.
B2, is the external transceiver VMO output, if the USB peripheral is setup to use this.
B1, and B0, are overridden by the SPI/I2C, and B4, by the SPI slave select if this is enabled.

You need to make sure your code disables all these, or the pins won't work.

Generally output_b should work fine. That it doesn't, suggests you have something wrong elsewhere.

Best Wishes
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 03, 2012 4:53 pm     Reply with quote

The code I'm using:
Code:
#include "18F4550.H"
#include <math.h>
#use standard_io(B)
#use delay (internal=1000000)
#fuses INTXT

#byte LATB=getenv("SFR:LATB")


int i = 500;
int z = 65536;
void main(void)         
{
LATB = 0b11001001;
delay_ms(5000);
output_high(PIN_B0);
delay_ms(500);
      while (1)      
      {
      for (i=65536;i>0;i=sqrt(i)){
         output_high(PIN_B0);
         output_high(PIN_B1);
         output_high(PIN_B2);
         output_high(PIN_B3);
         output_high(PIN_B4);
         output_high(PIN_B5);
         output_high(PIN_B6);
         output_high(PIN_B7);
         delay_us(i);
         z = 65536-i;
         output_low(PIN_B0);
         output_low(PIN_B1);
         output_low(PIN_B2);
         output_low(PIN_B3);
         output_low(PIN_B4);
         output_low(PIN_B5);
         output_low(PIN_B6);
         output_low(PIN_B7);
      delay_us(z);
      }
      }
}

I get one LED on after five seconds. Neither the for loop nor the LATB equals command do work. My goal is to create an array of bytes and shift them one after another on LATB. I would be really glad if somebody could provide me an example for the array too. Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 4:59 pm     Reply with quote

CCS uses data types that are not ANSI. Look in the CCS manual in this
section to see the sizes (and default sign) of data types:
Quote:

Basic and Special types

http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 03, 2012 5:44 pm     Reply with quote

I was there before I posted the other message. I read it. I didn't understand anything. Now I read it again. All I can see are some strange odd things like enum, union, struc. Nothing like a simple basic logic array. Is there no way to create a simple array of INT1? THAT sucks Evil or Very Mad Evil or Very Mad !
And I have no idea why the LATB equals "command" doesn't work. Do I need to set the TRISB register?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 5:56 pm     Reply with quote

The data types section says an 'int' is an unsigned 8-bit integer in CCS.
That's why your variables don't work. Change them to 'int16' and they
will work a lot better.

Actually there will still be a problem. I just noticed you are setting one
variable to 65536, which will require an 'int32' data type for the variable.
So use that.
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Tue Jan 03, 2012 6:19 pm     Reply with quote

Now I am using
Code:

int8 j[] = {10000000,00000001};
int32 i = 500;
int32 z = 65536;

what is working. But I don't know why LATB= doesn't work. Any ideas?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 03, 2012 6:39 pm     Reply with quote

Yes, it's because the power-on reset state of i/o pins is set to "input" by
the PIC's hardware. Putting a value in the LATB register doesn't
change the direction of the i/o pins.

You should be able to use output_b() and it will set the TRIS and LATB
for you.
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Wed Jan 04, 2012 3:06 am     Reply with quote

If I am using the following code, nothing at all happens. If I remove output_b, I'm able to see all LED dimming a bit. Any ideas?

Code:

#include "18F4550.H"
#include <math.h>
#use standard_io(B)
#use delay (internal=1000000)
#fuses INTXT

#byte LATB=getenv("SFR:LATB")

int8 j[] = {10000000,00000001};
int32 i = 500;
int16 k;
int32 z = 65536;

void main(void)         
{
output_b(0b11001001);
delay_ms(5000);
output_high(PIN_B0);
delay_ms(500);

      while (1)     
      {
      for (i=256;i>0;i=i/2){
         z = 256-i;
     for (k=0; k<1000; k++){
         output_high(PIN_B0);
         output_high(PIN_B1);
         output_high(PIN_B2);
         output_high(PIN_B3);
         output_high(PIN_B4);
         output_high(PIN_B5);
         output_high(PIN_B6);
         output_high(PIN_B7);
         delay_us(i);
         output_low(PIN_B0);
         output_low(PIN_B1);
         output_low(PIN_B2);
         output_low(PIN_B3);
         output_low(PIN_B4);
         output_low(PIN_B5);
         output_low(PIN_B6);
         output_low(PIN_B7);
      delay_us(z);
      }
     }
      }
}
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Wed Jan 04, 2012 11:40 am     Reply with quote

Thanks for helping.
It was now working with
Code:
int8 j[] = {10001000,01110001};
int32 i = 500;
int16 k;
int32 z = 65536;
void main(void)         
{
    while (1)   
    {
output_b(j[0]);
delay_ms(500);
output_b(j[1]);
delay_ms(500);
   }
}

which is exactly what I needed.
spark



Joined: 03 Jan 2012
Posts: 17

View user's profile Send private message MSN Messenger

PostPosted: Wed Jan 04, 2012 2:47 pm     Reply with quote

baaad...
Code:
#include "18F4550.H"
#include <math.h>
#use standard_io(B)
#use delay (internal=1000000)
#fuses INTXT

#byte LATB=getenv("SFR:LATB")

int8 j[8] = {10001000,01110001,01010001,01100001,01111001,01110101,01110011,01110000};
int16 i = 0;
int16 k;
int32 z = 65536;
void main(void)         
{
    while (1)   
    {
   for(i = 0; i<8; ++i){
   delay_ms(200);
   output_b(j[i]);
   }
   }
}

Does somebody have an idea why I'm not getting a sequence?
Thanks for helping.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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