View previous topic :: View next topic |
Author |
Message |
spark
Joined: 03 Jan 2012 Posts: 17
|
Setting LATCH outputs with 8 bit binary |
Posted: Tue Jan 03, 2012 2:01 pm |
|
|
Hi
I'm trying to set the outputs of a Port with an 8 bit number, so that it could look like
I'm using CCS compiler for the PIC18F4550.
I tried to use
but CCS came up with
Quote: | *** Error 12 "main.c" Line 131(6,10): Undefined identifier LATB |
Thanks for your help. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Tue Jan 03, 2012 3:21 pm |
|
|
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); |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 03, 2012 3:45 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9178 Location: Greensville,Ontario
|
|
Posted: Tue Jan 03, 2012 3:50 pm |
|
|
Be sure to have correct current limiting resistors with the LEDs, and they are wired properly ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19384
|
|
Posted: Tue Jan 03, 2012 4:02 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Tue Jan 03, 2012 4:53 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Tue Jan 03, 2012 5:44 pm |
|
|
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 !
And I have no idea why the LATB equals "command" doesn't work. Do I need to set the TRISB register? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 03, 2012 5:56 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Tue Jan 03, 2012 6:19 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 03, 2012 6:39 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Wed Jan 04, 2012 3:06 am |
|
|
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);
}
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Wed Jan 04, 2012 11:40 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
spark
Joined: 03 Jan 2012 Posts: 17
|
|
Posted: Wed Jan 04, 2012 2:47 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|