|
|
View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 10, 2013 1:14 am |
|
|
Try changing the i2c speed in main(), using the macro shown in the
example below:
Code: |
#include <18F67K22.h>
#fuses HSH, NOWDT, PUT, BROWNOUT
#use delay(clock=20M)
#use i2c(Master,Fast=400000,sda=PIN_D5,scl=PIN_D6,FORCE_HW,stream=I2C_POWER)
// This macro sets the hardware i2c baudrate for SSP2.
#byte SSP2ADD = getenv("SFR:SSP2ADD")
#byte SSP2CON1 = getenv("SFR:SSP2CON1")
#define set_i2c_speed2(baud) \
SSP2CON1 = 0x28; \
SSP2ADD = (((getenv("CLOCK") + (baud *2))/ (baud * 4)) -1)
//==========================================
void main()
{
// Examples of how to call it.
set_i2c_speed2(100000);
set_i2c_speed2(400000);
while(1);
} |
This macro first puts the MSSP in a mode such that SSP2ADD is treated
as the baudrate register. Then it calculates the baudrate value, and
rounds off the result, and then it writes it to the baudrate register.
Why not use i2c_speed() ? Because I'm not sure CCS is doing it correctly. |
|
|
DireSpume
Joined: 07 Jan 2013 Posts: 31
|
|
Posted: Wed Apr 10, 2013 12:13 pm |
|
|
Even though I was 99.9% sure my oscillator was working as expected, I did double check it as you suggested, Ttelmah. Using FAST_IO, I was able to get 1.25 MHz on an output pin. The assembly code consists is 3 instructions, taking 4 cycles to complete = 20MHz/(4*4) = 1.25 MHz. So now I'm 100% sure the oscillator is working as expected, so that's good. (Incidentally, I did initially have problems with the fuses at first, as you suspected, and it defaulted to the internal oscillator (which is 8 MHz on the 18F67K22)).
PCM Programmer, your method worked! Any idea why the back-door method works but the normal method (i.e. Fast=100000) doesn't? Also, I tried the built-in i2c_speed() function, but that caused it to crash on i2c_start() for some reason. I'm both delighted to have a workaround and annoyed that I don't understand why it's necessary.
Also, when I use your macro at 100000, the actual frequency is around 92 kHz, and at 400000, the actual frequency is 285 kHz. I know the clock frequency has only certain discrete settings, but does this sound about right? Thanks for your help guys! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 10, 2013 12:38 pm |
|
|
Post your test program which you use to measure the SCL clock frequency. |
|
|
|
|
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
|