|
|
View previous topic :: View next topic |
Author |
Message |
Electronguru
Joined: 26 May 2013 Posts: 11
|
PIC18f4523 code |
Posted: Mon May 27, 2013 12:03 am |
|
|
Hi ,
I am trying to run this code on 18F4523 , but can't seem to get anything out of it. It programs correctly using PICkit2. I don't get any clock output on pin RA6, or pin 25 (TX), or pin 18 which is SCL output ??? I am trying to use internal oscillator, since there is not external oscillator connected.
Here's the code:
Code: |
#include <18F4523.h> // PICF4523 header file
#device ADC=12 // 12 bits ADC
#use delay(clock=4000000) // for 4Mhz crystal
#fuses NOWDT, NOPROTECT, NOLVP // for debug mode
#FUSES INTRC //Internal Osc
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, Parity=N, Bits=8, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
int d;
void main()
{
int8 data[8] = {0x1A,0x1B,0x2C,0x3D,0x4E,0x5F};
i2c_start();
i2c_write(0xA1);
i2c_write(0xA2);
i2c_write(0xB1);
i2c_write(0xC1);
i2c_write(0xF1);
data[0] = i2c_read();
data[1] = i2c_read();
data[2] = i2c_read();
data[3] = i2c_read();
data[4] = i2c_read();
data[5] = i2c_read(0);
i2c_stop();
delay_ms(10);
while(1)
{
// JUST FOR TESTING
i2c_start();
i2c_write(0xA1);
i2c_write(0xA2);
i2c_write(0xB1);
i2c_write(0xC1);
i2c_write(0xF1);
delay_ms(100);
//
putc(data[0]);
putc(data[1]);
putc(data[2]);
putc(data[3]);
putc(data[4]);
putc(data[5]);
printf("%x\n",data[0]);
printf("%x\n",data[1]);
printf("%x\n",data[2]);
printf("%x\n",data[3]);
printf("%x\n",data[4]);
printf("<<18F4523 is ready>>\n");
delay_ms(10);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 27, 2013 12:11 am |
|
|
Quote: | I don't get any clock output on pin RA6, or pin 25 (TX), or pin 18 which is SCL output ? |
That's what would happen if you were missing the 10K pullup resistor on the MCLR pin. |
|
|
Electronguru
Joined: 26 May 2013 Posts: 11
|
|
Posted: Mon May 27, 2013 1:08 am |
|
|
PCM programmer wrote: | Quote: | I don't get any clock output on pin RA6, or pin 25 (TX), or pin 18 which is SCL output ? |
That's what would happen if you were missing the 10K pullup resistor on the MCLR pin. |
Thanks for the tip, there was a 10K resistor but I was leaving the MCLR pin connected to PICkit2 and it was pulling it down. Now I do see clock output at RA6.
But no RS232 signals at pin 25 or I2c clk on pin 18 ??
Although this code simulated as expected in Proteus. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon May 27, 2013 10:37 am |
|
|
work better ???
at least for clock out
also consider using
setup_oscillator() per the header file
and
BTW you might want to specify a few more
#fuses
based on what you want to accomplish |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 27, 2013 12:19 pm |
|
|
asmboy wrote: |
work better ???
at least for clock out
|
INTRC is the correct fuse to get a Fosc/4 clock signal on pin A6.
If you use INTRC_IO, the two oscillator pins (A6 and A7) are i/o pins.
If you have the CCS IDE, the fuses.txt file has this:
Quote: | INTRC Internal RC Osc
INTRC_IO Internal RC Osc, no CLKOUT |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 27, 2013 12:58 pm |
|
|
Quote: | But no RS232 signals at pin 25 or I2c clk on pin 18 ??
|
Do you have pull-up resistors on the SDA and SCL lines ?
If you don't have pull-ups, the i2c_write() function will probably lock-up.
That's because i2c_write() polls the SCL line and if it's low (ie., checking
for clock stretching from the slave), the code will wait in a loop until it
goes high. If there are no pull-ups it will never go high - it will "hang" the
program. This is also true for i2c_read().
Because you have all these i2c lines before your printf statements, I
strongly suspect that you are missing the pull-ups and the program
never gets to the printfs.
Quote: | Although this code simulated as expected in Proteus. |
Proteus doesn't care about many hardware issues. Because of this,
many things may work in Proteus but they don't work on a real board.
On a real board, you have to make everything correct. |
|
|
Electronguru
Joined: 26 May 2013 Posts: 11
|
|
Posted: Tue May 28, 2013 1:12 am |
|
|
Hi,
PCM Programmer, yes you right to point out, I don't have pull ups on I2C. I will put the pull ups now and see if both I2C and RS232 work.
Regards |
|
|
|
|
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
|