RTC and MMC and interrupt
RTC and MMC and interrupt
I am using 18F452 PIC and I connected the RTC to port A and the MMC to the normal SPI port I read continuosly from the mmc and send the data to UART port and I tried to read from the RTC the reading is almost error but if i stopped the interrupt the RTC work very nice? anybody can help why?
update :
after many invistigations i found that (Mmc_Fat_Assign(&filename, 0)) make the soft i2c no function this is my code
after many invistigations i found that (Mmc_Fat_Assign(&filename, 0)) make the soft i2c no function this is my code
Code: Select all
/*
this program for second controller in lighting control project
* the program takes data parallel through PORTA from first controller
* send the recived data serially through PORTA pin0
* send sync pulse at the begining of each byte
last update date 12-04-2007
*/
#include <built_in.h>
unsigned short sssss[4],see,mte,hhe,dde,moe,yye,ini_ok=0,cc,tnum[5],ses,mts,hhs,dds,mos,yys,dcount,set_flag=0,dis=0,dis_flag=0;
unsigned short keytemp,ini_fl,J,fr_fl,key_byt=0,key1=0,slave_no=255,slave_change_flag=0,scano=0xe0,scani=0,scan_fl=0,ret_no,temp_del=0;
unsigned short keyl=0,keyl1=0,prog_no,nocnt=0,frcnt=0,oldstate=0,col_type=1,re=0,gr=0,bl=0,scan_temp;
unsigned bcnt=512,col=0;
char filename[14]= "BEEBUL01BIN",txt[4];
unsigned long size,size_temp=0;
const char ser[]="Aoday H Mohamed";
void Zero_Fill(char *value) { // fill text repesentation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}//~
char *R_Trim(char *str1){
while (*str1 == ' ')
str1++;
return str1;
}//~
void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
char *tc;
ByteToStr(day, tnum); // day
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,1, tc);
Lcd_Chr_Cp('-');
ByteToStr(mn, tnum); // month
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out_cp(tc);
Lcd_Chr_Cp('-');
ByteToStr(year, tnum); // month
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out_cp(tc);
LCD_Out_cp(" ");
/*if (hr>12) {
ByteToStr(hr-12,tnum); // hour\
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,14,tc);
} else {
if (hr==0){
LCD_Out(1,14,"12");
}else{ */
ByteToStr(hr,tnum); // hour
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,11,tc);
// }
// }
Lcd_Chr_Cp(':');
ByteToStr(mnt,tnum); // minute
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out_cp(tc);
// Lcd_Out(1,12," ");
/* if (hr<12) {
Lcd_Out(1,19,"AM");
} else {
Lcd_Out(1,19,"PM");
}
ByteToStr(sec,tnum); // seconds
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,12,tc);*/
}//~
void Read_RTC() {
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
sssss[1]=88;
see = Soft_I2C_Read(1) & 127;
mte = Soft_I2C_Read(1) & 127;
hhe = Soft_I2C_Read(1) & 63;
dde = Soft_I2C_Read(1) & 63;
dde = Soft_I2C_Read(1) & 63;
moe = Soft_I2C_Read(1) & 31;
yye = Soft_I2C_Read(0);
I2C_Stop();
see = ((see & 0xF0) >> 4)*10 + (see & 0x0F);
mte = ((mte & 0xF0) >> 4)*10 + (mte & 0x0F);
hhe = ((hhe & 0xF0) >> 4)*10 + (hhe & 0x0F);
yye = ((yye & 0xF0) >> 4)*10 + (yye & 0x0F);
dde = ((dde & 0xf0) >> 4)*10 + (dde & 0x0F);
moe = ((moe & 0xf0) >> 4)*10 + (moe & 0x0F);
}//~
void col_print() {
if (slave_change_flag) {
switch (col_type) {
case 1:{
Lcd_Out(2,1,"Red: ");
ByteToStr(re,txt);
Lcd_Out(2,10,txt);
break;
}
case 2:{
Lcd_Out(2,1,"Green: ");
ByteToStr(gr,txt);
Lcd_Out(2,10,txt);
break;
}
case 3:{
Lcd_Out(2,1,"Blue: ");
ByteToStr(bl,txt);
Lcd_Out(2,10,txt);
break;
}
}
}
col=bl*0x400+gr*0x20+re;
// b1:=bc*$400+gc*$20+rc
}
unsigned short Keypad() {
PORTD=scano;
scani=PORTD & 0x0F;
if ((scani == 0x0f) && (scano==scan_temp)) {
scan_fl=0;
}
if ((scani != 0x0f) && (scan_fl==0)) {
scan_fl=1;
scan_temp=scano;
ret_no= scano | scani;
} else ret_no=255;
scano=scano<<1;
if (scano!=0xe0) scano=scano | 0x10;
return ret_no;
}
void main_init() {
TRISB = 0; // PORTB is output
TRISE = 0; // PORTE is output
TRISD = 0x0f; // PORTD is output
TRISA = 0; // PORTA is output
PORTB = 0;
PORTA = 0;
TMR0L = 96;
ini_fl = 0;
sssss[1]=41;
Usart_Init(115200); // init the USART at 38400 baude casue *4
Delay_1sec();
T0CON = 0xC2; // Assign prescaler to TMR0
ADCON1=7;
Delay_1sec();
Soft_I2C_Config(&PORTA, 0,1); // initialize I2C
// I2C_Init(100000); // initialize full master mode
}
void interrupt() {
if (ini_fl==0) {
ini_fl=1;
Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
Lcd_Init(&PORTB);
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1," PowerMicro");
Lcd_Out(2,1," Master control");
//Delay_1sec();
Lcd_Cmd(LCD_CLEAR);
Lcd_Out(1,1,"Please Wait....");
Lcd_Out(2,1,"Now Prog. No.");
prog_no = Eeprom_Read(0);
filename[6]=prog_no/10+0x30;
filename[7]=prog_no%10+0x30;
ByteToStr(prog_no,txt);
Lcd_Out(2,14,txt);
if (Mmc_Fat_Init(&PORTC, 2) == 0)
{
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
if (Mmc_Fat_Assign(&filename, 0))
{
// Mmc_Fat_Reset(&size);
// Lcd_Out(1,1,"Press Program No.");
}else {
/* Lcd_Out(1,1,"There is no Data");
prog_no=1;
Eeprom_Write(0,prog_no);
key1=0;
ini_fl=0;
*/ }
} else Lcd_Out(1,1,"No MMC card ");
//Soft_I2C_Config(&PORTA, 0,1); // initialize I2C
}
Read_RTC();
Display_Time(see,mte,hhe,dde,moe,yye);
/* bcnt++;
if (bcnt==512) bcnt=0;
//Mmc_Fat_Read(&J);
//Usart_Write(j);
//size--;
size_temp++;
if (size_temp>=size) {
bcnt=0;
//Mmc_Fat_Assign(&filename,0);
//Mmc_Fat_Reset(&size);
size_temp=0;
temp_del++;
//Display_Time(see,mte,hhe,dde,moe,yye);
ByteToStr(temp_del,txt);
Lcd_Out(2,1,txt);
}
Read_RTC();
Delay_100ms();
ByteToStr(mte,txt);
Lcd_Out(2,5,txt);
if (keyl1==1) {
keyl1=0;
switch (key_byt) {
case 126: {
if (!slave_change_flag) {
key_byt = 65;
prog_no=key1;
Eeprom_Write(0,prog_no);
key1=0;
ini_fl=0;
} else {
slave_no=key1;/// slave change
Lcd_Out(1,1,"slave : ");
ByteToStr(key1,txt);
Lcd_Out(1,10,txt);
col_print();
}
break;
} // A
case 119: key_byt = 1; break; // 1
case 123: key_byt = 2; break; // 2
case 125: key_byt = 3; break; // 3
case 183: key_byt = 4; break; // 4
case 187: key_byt = 5; break; // 5
case 189: key_byt = 6; break; // 6
case 190: {
slave_change_flag=~slave_change_flag;
if (slave_change_flag) {
Lcd_Out(1,1," Press slave No:");
key1=0;
} else {
Lcd_Out(1,1,"Press Program No");
key1=0;
}
key_byt = 66;
break; // B // uncomment this block for keypad4x4
}
case 215: key_byt = 7; break; // 7
case 219: key_byt = 8; break; // 8
case 221: key_byt = 9; break; // 9
case 222: {
key_byt = 67;
key1=0;
Lcd_Out(1,1,"Press Program No.");
break;
} // C
case 231: {
switch (col_type) {
case 1:{
re--;
if (re==255) re=0;
break;
}
case 2:{
gr--;
if (gr==255) gr=0;
break;
}
case 3:{
bl--;
if (bl==255) bl=0;
break;
}
}
col_print();
key_byt = 42;
break; // *
}
case 235: key_byt = 0; break; // 0
case 137: {
switch (col_type) {
case 1:{
re++;
if (re>=32) re=31;
break;
}
case 2:{
gr++;
if (gr>=32) gr=31;
break;
}
case 3:{
bl++;
if (bl>=32) bl=31;
break;
}
}
col_print();
key_byt = 35;
break; // #
}
case 238: {
col_type++;
if (col_type==4) col_type=1;
col_print();
key_byt = 68;
break; // D
}
}
if ((key_byt>=0) && (key_byt<=9))
{
if (key1>=10) key1=key1 % 10;
key1=(key1 * 10) + key_byt;
if (slave_change_flag==0) {
if (key1>=100) key1=0;
Lcd_Out(1,1,"Now Change to");
ByteToStr(key1,txt);
Lcd_Out(1,14,txt);
} else {
// slave_no=key1;/// slave change
}
}
// keyl1=0;
}
TMR0L = 0xd0;
// TMR0L = 0x0;
INTCON.T0IF = 0; // Set T0IE, clear T0IF*/
}//~
void main() {
main_init();
INTCON = 0xA0; // Enable TMRO interrupt
do {
asm {
clrwdt;
}
keytemp=Keypad();
if (keytemp!=255)
{
// keytemp=1;
keyl1=1;
key_byt=ret_no;
//ret_no=255;
}
} while(1); // endless loop
}//~!
the bellow functions using port A as follows
RA0 Data
RA1 clock
and you must put the following instructions in initializing
and bellow the example program how to read the time
bellow example program for set the time
RA0 Data
RA1 clock
Code: Select all
void Soft_s_I2C_Start(){
PORTA.f0=1;
PORTA.f1=1;
Delay_10us();
PORTA.f0=0;
Delay_1us();
Delay_1us();
PORTA.f1=0;
}//~
void Soft_s_I2C_Write(char b){
char temp_count=0;
for (temp_count=0;temp_count<8;temp_count++){
PORTA.f0=b.f7;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
b=b<<1;
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=1;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
TRISA.f0=0;
}//~
char Soft_s_I2C_Read(char b){
char temp_count=0,temp_da=0;
TRISA.f0=1;
for (temp_count=0;temp_count<8;temp_count++){
temp_da=temp_da<<1;
temp_da.f0=PORTA.f0;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=0;
PORTA.f0=~(b.f0);
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
return(temp_da);
}//~
void Soft_s_I2C_Stop() {
PORTA.f0=0;
PORTA.f1=1;
Delay_10us();
PORTA.f0=1;
}//~
Code: Select all
ADCON1=7;
TRISA = 0; // PORTA is output
Code: Select all
unsigned char sec, mnt, hr, day, mn, year;
char *txt, tnum[4];
void Soft_s_I2C_Start(){
PORTA.f0=1;
PORTA.f1=1;
Delay_10us();
PORTA.f0=0;
Delay_1us();
Delay_1us();
PORTA.f1=0;
}//~
void Soft_s_I2C_Write(char b){
char temp_count=0;
for (temp_count=0;temp_count<8;temp_count++){
PORTA.f0=b.f7;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
b=b<<1;
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=1;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
TRISA.f0=0;
}//~
char Soft_s_I2C_Read(char b){
char temp_count=0,temp_da=0;
TRISA.f0=1;
for (temp_count=0;temp_count<8;temp_count++){
temp_da=temp_da<<1;
temp_da.f0=PORTA.f0;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=0;
PORTA.f0=~(b.f0);
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
return(temp_da);
}//~
void Soft_s_I2C_Stop() {
PORTA.f0=0;
PORTA.f1=1;
Delay_10us();
PORTA.f0=1;
}//~
char *R_Trim(char *str1){
while (*str1 == ' ')
str1++;
return str1;
}//~
void Zero_Fill(char *value) { // fill text repesentation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}//~
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time(char *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
Soft_s_I2C_Start();
Soft_s_I2C_Write(0xd0);
Soft_s_I2C_Write(0);
Soft_s_I2C_Start();
Soft_s_I2C_Write(0xd1);
*sec = Soft_s_I2C_Read(1) & 127;
*mnt = Soft_s_I2C_Read(1) & 127;
*hr = Soft_s_I2C_Read(1) & 63;
*day = Soft_s_I2C_Read(1) & 63;
Soft_s_I2C_Read(1) & 63;
*mn = Soft_s_I2C_Read(1) & 31;
*year = Soft_s_I2C_Read(0);
Soft_s_I2C_Stop();
}//~
//-------------------- Formats date and time
void Transform_Time(char *sec, char *mnt, char *hr, char *day, char *mn, char *year) {
*sec = ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);
*mnt = ((*mnt & 0xF0) >> 4)*10 + (*mnt & 0x0F);
*hr = ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F);
*year = ((*year & 0xF0) >> 4)*10 + (*year & 0x0F);
*day = ((*day & 0xf0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0xf0) >> 4)*10 + (*mn & 0x0F);
}//~
//-------------------- Output values to LCD
void Display_Time(char sec, char mnt, char hr, char day, char mn, char year) {
char *tc;
ByteToStr(day, tnum); // day
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,6, tc);
ByteToStr(mn, tnum); // month
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,9, tc);
ByteToStr(year, tnum); // month
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(1,14, tc);
// LCD_Chr(1,15,52+year); // year
ByteToStr(hr,tnum); // hour
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,6,tc);
ByteToStr(mnt,tnum); // minute
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,9,tc);
ByteToStr(sec,tnum); // seconds
tc = R_Trim(tnum);
Zero_Fill(tc);
LCD_Out(2,12,tc);
}//~
//------------------ Performs project-wide init
void Init_Main() {
Delay_100ms();
LCD_Init(&PORTb); // initialize LCD on portd
Delay_8ms();
ADCON1=7;
Soft_I2C_Config(&PORTA, 0,1); // initialize I2C
// Soft_I2C_Init(&PORTC);
txt = "Date:"; // prepare and output static text on LCD
LCD_Out(1,1,txt);
LCD_Chr(1,8,':');
LCD_Chr(1,11,':');
txt = "Time:";
LCD_Out(2,1,txt);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
txt = "20";
LCD_Out(1,12,txt);
LCD_Cmd(LCD_CURSOR_OFF); // cursor off
Soft_s_I2C_Start();
Soft_I2C_Write(0xd0);
Soft_I2C_Write(0x0);
Soft_I2C_Write(0x59);
Soft_s_I2C_Stop();
}//~
//----------------- Main procedure
void main() {
Init_Main(); // perform initialization
while (1) {
// Soft_s_I2C_Write(0x55);
// Delay_8ms();
Read_Time(&sec,&mnt,&hr,&day,&mn,&year); // read time from RTC(PCF8583)
Transform_Time(&sec,&mnt,&hr,&day,&mn,&year); // format date and time
Display_Time(sec, mnt, hr, day, mn, year); // prepare and display on LCD
// Delay_ms(1000); // wait 1s
/* Delay_ms(10000); // wait 1s
Delay_ms(10000); // wait 1s
Delay_ms(10000); // wait 1s
Delay_ms(10000); // wait 1s
Delay_ms(10000); // wait 1s
Delay_ms(10000); */ // wait 1s
}
}
Code: Select all
void Soft_s_I2C_Start(){
PORTA.f0=1;
PORTA.f1=1;
Delay_10us();
PORTA.f0=0;
Delay_1us();
Delay_1us();
PORTA.f1=0;
}//~
void Soft_s_I2C_Write(char b){
char temp_count=0;
for (temp_count=0;temp_count<8;temp_count++){
PORTA.f0=b.f7;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
b=b<<1;
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=1;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
TRISA.f0=0;
}//~
char Soft_s_I2C_Read(char b){
char temp_count=0,temp_da=0;
TRISA.f0=1;
for (temp_count=0;temp_count<8;temp_count++){
temp_da=temp_da<<1;
temp_da.f0=PORTA.f0;
Delay_1us();
PORTA.f1=1;
// Delay_10us();
PORTA.f1=0;
Delay_1us();
}
TRISA.f0=0;
PORTA.f0=~(b.f0);
Delay_1us();
PORTA.f1=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
PORTA.f1=0;
return(temp_da);
}//~
void Soft_s_I2C_Stop() {
PORTA.f0=0;
PORTA.f1=1;
Delay_10us();
PORTA.f0=1;
}//~
void main() {
ADCON1=6;
// Soft_I2C_Config(&PORTA, 0,1); // initialize full master mode
TRISA=0x0;
// while (1) {
//Soft_s_I2C_Write(0x55);
//PORTA.f1=1;
//Delay_8ms();
//PORTA.f1=0;
// }
// Soft_I2C_Init(&PORTD);
Soft_s_I2C_Start(); // issue start signal
Soft_s_I2C_Write(0xd0); // address PCF8583
Soft_s_I2C_Write(0); // start from word at address 0 (configuration word)
Soft_s_I2C_Write(0x20); // write $80 to config. (pause counter...)
Soft_s_I2C_Stop(); // issue stop signal
Soft_s_I2C_Start(); // issue start signal
Soft_s_I2C_Write(0xd0); // address PCF8583
Soft_s_I2C_Write(0); // start from word at address 0 (configuration word)
Soft_s_I2C_Write(0); // write 0 to seconds word
Soft_s_I2C_Write(0x08); // write $30 to minutes word
Soft_s_I2C_Write(0x17); // write $11 to hours word
Soft_s_I2C_Write(0x22); // write $30 to year/date word
Soft_s_I2C_Write(0x21); // write $08 to weekday/month
Soft_s_I2C_Write(0x03); // write $08 to weekday/month
Soft_s_I2C_Write(0x08); // write $08 to weekday/month
Soft_s_I2C_Stop(); // issue stop signal
Soft_s_I2C_Start(); // issue start signal
Soft_s_I2C_Write(0xd1); // address PCF8530
Soft_s_I2C_Write(0); // start from word at address 0
Soft_s_I2C_Write(0); // write 0 to config word (enable counting)
Soft_s_I2C_Stop(); // issue stop signal
}//~!
I'm doing a project that is same with Aoday's project.
I am using MikroBasic 6.0 and got same problem with Aoday.
(My project is using Pic18f452 8MHz, 16x2 LCD 4 bits, DS1307 soft i2c, and MMC spi, got wrong time and date data from DS1307, LCD Show: Time = 3?:7?:7?, Date: = 1?-??-??)
I'm looking for a good way to solve it.
Thanks for your code, Aoday's
ME team should know how to solve it.
I am using MikroBasic 6.0 and got same problem with Aoday.
(My project is using Pic18f452 8MHz, 16x2 LCD 4 bits, DS1307 soft i2c, and MMC spi, got wrong time and date data from DS1307, LCD Show: Time = 3?:7?:7?, Date: = 1?-??-??)
I'm looking for a good way to solve it.
Thanks for your code, Aoday's
ME team should know how to solve it.
Hi,
I apologize for a late response.
first code posted by Aoday has 2 problems:
1) interrupt routine contains time-consuming code. This is just warning.
2) Software I2C Library can't be used together with interrupts.
Help -> Software I2C Library:
Note: This library implements time-based activities, so interrupts need to be disabled when using Soft I²C.
Simply, if you use Delays for some code - then interrupts will corrupt them.
Please provide us with example code that shows problem and we will examine it.
You should note that Aoday's library is tested without using interrupts,
our Library works fine under the same circumstances.
Also, our Library can use any pair of MCU pins,
see Soft_I2C_Config explanation in Help.
All of these facts are same for mikroBasic.
Once again Software I2C Library passed our tests, but if you find some problem,
provide us with example code and we will be happy to solve it.
I apologize for a late response.
first code posted by Aoday has 2 problems:
1) interrupt routine contains time-consuming code. This is just warning.
2) Software I2C Library can't be used together with interrupts.
Help -> Software I2C Library:
Note: This library implements time-based activities, so interrupts need to be disabled when using Soft I²C.
Simply, if you use Delays for some code - then interrupts will corrupt them.
We tested Software I2C Library many times and we found no errors.radiofm wrote:Hi
Thanks for your i2c soft !
there is something wrong with soft i2c in mikroC ver 8.1 !!!
Please provide us with example code that shows problem and we will examine it.
You should note that Aoday's library is tested without using interrupts,
our Library works fine under the same circumstances.
Also, our Library can use any pair of MCU pins,
see Soft_I2C_Config explanation in Help.
All of these facts are same for mikroBasic.
Once again Software I2C Library passed our tests, but if you find some problem,
provide us with example code and we will be happy to solve it.
hello.milan wrote:
We tested Software I2C Library many times and we found no errors.
Please provide us with example code that shows problem and we will examine it.
1. i sent to ME my program via support desk
2. In my program first starting soft i2c and next after that i enable interrupts.
also before soft i2c i disable ALL interrupts.
3. with Aoday i2c soft routine my program working perfectly but with i2c soft library form mikroC unfortunetly not.
Best Regards
Robert
HI
try with this (from AODAY example)
you must only do some modifications in code - changing PINS
for example my working sample of code
try with this (from AODAY example)
you must only do some modifications in code - changing PINS
for example my working sample of code
Code: Select all
//first define sda and scl
#define soft_SDA PORTD.F2
#define soft_SCL PORTD.F3
#define trisd_SDA TRISD.F2
#define trisd_SCL TRISD.F3
//functions
void Soft_s_I2C_Start(){
soft_SDA=1;
soft_SCL=1;
Delay_10us();
soft_SDA=0;
Delay_1us();
Delay_1us();
soft_SCL=0;
}//~
void Soft_s_I2C_Write(char b){
char temp_count=0;
for (temp_count=0;temp_count<8;temp_count++){
soft_SDA=b.f7;
Delay_1us();
soft_SCL=1;
// Delay_10us();
b=b<<1;
soft_SCL=0;
Delay_1us();
}
trisd_SCL=1;
Delay_1us();
soft_SCL=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
soft_SCL=0;
trisd_SDA=0;
}//~
char Soft_s_I2C_Read(char b){
char temp_count=0,temp_da=0;
trisd_SDA=1;
for (temp_count=0;temp_count<8;temp_count++){
temp_da=temp_da<<1;
temp_da.f0=soft_SDA;
Delay_1us();
soft_SCL=1;
// Delay_10us();
soft_SCL=0;
Delay_1us();
}
trisd_SDA=0;
soft_SDA=~(b.f0);
Delay_1us();
soft_SCL=1;
// Delay_10us();
// Delay_10us();
Delay_1us();
Delay_1us();
Delay_1us();
soft_SCL=0;
return(temp_da);
}//~
void Soft_s_I2C_Stop() {
soft_SDA=0;
soft_SCL=1;
Delay_10us();
soft_SDA=1;
}//~
// sample working i2c code:
void main()
{
.......
.......
.......
.......
//start ds1882
Soft_s_I2C_Start();
Soft_s_I2C_Write(0x50); //adres
Soft_s_I2C_Write(0x45); //01
delay_ms(70);
Soft_s_I2C_Write(0x05); //00
delay_ms(70);
Soft_s_I2C_Write(0x87); //reg
Soft_s_I2C_Stop();
.....
......
}