|
|
View previous topic :: View next topic |
Author |
Message |
jjacob
Joined: 08 Mar 2008 Posts: 54 Location: PORTUGAL (PORTO)
|
18F26K22 (I2C + PWM) = Conflict |
Posted: Thu Oct 19, 2017 8:48 am |
|
|
Hello,
I want to use I2C and PWM at same time, but this is not working.
I have two slaves on i2c bus, an lcd driver (brightness is controlled by PWM) and a ds3231 rtc. So, if use only PWM to control brightness it works all fine. If I use only ds3231 it works well. But, if join brightness control (PWM) and rtc code, rtc hangs on " communication_status = ds3231_read_configuration();"
When testing all together, if duty cycle is at 0%, all works. If I set duty cycle to 50%, then rtc hangs on the read configuration.
Someone understands this? I need help... thanks.
I'm using PCH Compiler 5.070
Code: | #include <18F26K22.h>
#device adc=10
#FUSES INTRC_IO // INTRC_IO Internal RC Osc, no CLKOUT
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOPUT //No Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOMCLR //Master Clear pin NOT enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES CCP2C1
#use delay(clock = 16000000)
#use rs232(stream = rede, FORCE_SW, baud = 9600, BITS = 8, PARITY = N, STOP = 1, xmit = PIN_C6, rcv = PIN_C7)
#use TIMER(TIMER=1, TICK=1ms, BITS=16, NOISR)
#use I2C(Master, I2C2, slow) // Its using the second slot of i2c
#include "ds3231_v1.3.c"
// variables
unsigned int32 current_tick;
unsigned int32 previous_tick_button;
unsigned int32 previous_tick_menu_exit;
// functions
unsigned int16 tick_difference(unsigned int16 current, unsigned int16 previous) {
return (current - previous);
}
void main(void) {
int h, min, s, dw, dm, m, y, communication_status;
float temperature;
unsigned int16 pwm_value = 0;
// The PWM as 10bits of resolution when midle number is "255"
setup_timer_2(T2_DIV_BY_4, 255, 1); // 16000000/((255+1)*4*4) = 3906 hz
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
//
// Set Brightness - set values from 0 - 1024
// MIN: 1023 - 0%
// MID: 512 - 50%
// MAX: 0 - 100%
// Its Inverted
pwm_value = 1023; // use a variable type long int16, to avoid use constante number like "512L"
set_pwm1_duty(pwm_value); //
pwm_value = 1023;
set_pwm2_duty(pwm_value); //
delay_ms(100);
communication_status = ds3231_read_configuration();
if (communication_status) {
fprintf(rede, "\n\r1 - error when communicate with ds3231.");
// ###################
// send ERROR to display
}
current_tick = previous_tick_menu_exit = get_ticks();
while (1) {
current_tick = get_ticks();
if (tick_difference(current_tick, previous_tick_menu_exit) > 1000) {
previous_tick_menu_exit = current_tick; // reset to interval
// Reading ds3231 temperature
ds3218_read_temperature(temperature);
fprintf(rede, "\n\n\rtempRTC: %.2f", temperature);
// Get time
ds3231_read_time(h, min, s);
fprintf(rede, "\n\r%02u:%02u:%02u", h, min, s);
}
}
} |
Code: | int ds3231_read_configuration() {
int _status;
delay_us(bus_free_time);
i2c_start();
_status = i2c_write(_ds3231_wadd);
_status |= i2c_write(_ds3231_control_reg);
i2c_start();
_status |= i2c_write(_ds3231_radd);
_ds3231_shadow_ctrlreg = i2c_read(_ack);
_en32khz_shadow = i2c_read(_nack);
i2c_stop();
_en32khz_shadow &= 0x08;
_ds3231_shadow_ctrlreg &= 0xDF; // leave thee CONV bis as zero wich is its normal state
return(_status);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Oct 19, 2017 9:33 am |
|
|
CCP1 and CCP2 on this are ECCP's, which means they have pulse steering, so OR in CCP_PULSE_STEERING_A with your PWM setup, which should force the outputs to the PxA pins only. |
|
|
jjacob
Joined: 08 Mar 2008 Posts: 54 Location: PORTUGAL (PORTO)
|
|
Posted: Thu Oct 19, 2017 9:41 am |
|
|
Dear Ttelmah,
Thanks for your help... It solved my problem.
Code: |
// The PWM as 10bits of resolution when midle number is "255"
setup_timer_2(T2_DIV_BY_4, 255, 1); // 16000000/((255+1)*4*4) = 3906 hz
setup_ccp1(CCP_PWM | CCP_PULSE_STEERING_A);
setup_ccp2(CCP_PWM | CCP_PULSE_STEERING_A);
|
|
|
|
|
|
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
|