Problem with codevision and I2C

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

HI,

 

 

I used to work with a project that uses tiny2313 and I2C DTMF encoder PCD3312. I had no problem with that, until I decide to combine it with I2C LCD (David help me a lot in the past) with those libraries. Please, notice that I2C LCD libraries work perfect, alone. When I combine i2c.h with lcd_i2c.h, LCD work fine but PCD3312 sends music tones instead of DTMF.

(PCD3312 and PCF8574 have different addresses).

 

 

 

 

Any ideas ?

 

 

Thank You and happy new year !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post your code.    It looks as if the PCD3312 requires a 3.58MHz crystal.   And you send it the relevant I2C commands to select musical or DTMF.

 

Since you are using a Tiny2313,   you must be using the <lcd_i2c.h> library which in turn depends on the CV <i2c.h> bit-bash library.

 

In terms of interaction.    You obviously only talk to one I2C slave at any one time.   So if you are sending a command to the PCD3312,   it will always have an i2c_stop() before you write to the LCD.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Hi David,

 

Please notice:

 

1) Initially the project had a parallel LCD, and PCD3312 use to send DTMF tones, correctly.So, PCD3312 commands (clock,XTAL enable and DTMF instead of music tone control data, are ok.

2) Now, CV accept lcd_i2c.h instead of alcd.h.From that point, PCD3312 send tones but not DTMF. Probably there is a conflict because I use i2c and lcd_i2c, together.

 

 

 

 


Project : Telephone Dialler
Version : 
Date    : 27/12/14

Chip type               : ATtiny4313
AVR Core Clock frequency: 2,000000 MHz
Memory model            : Tiny
External RAM size       : 0
Data Stack size         : 32
*****************************************************/
#include <tiny4313.h>
#include <stdio.h>
#include <delay.h>
#include <ctype.h>
#include <stdlib.h>
#include <lcd_i2c.h>

// I2C Bus functions
#include <i2c.h>   

// Alphanumeric LCD functions
//#include <alcd.h>     (Old Parallel LCD)

#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif
      
#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
eeprom char flag_open=0x39;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
flag_open=0x38;
}   
// USART Receiver buffer
#define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE];

#if RX_BUFFER_SIZE <= 256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif

// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;

status=UCSRA;
data=UDR;

if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {  
   rx_buffer[rx_wr_index++]=data;
#if RX_BUFFER_SIZE == 256
   //special case for receiver buffer size=256
   if (++rx_counter == 0) rx_buffer_overflow=1;
#else
   if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      }
#endif
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index++];
#if RX_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#endif
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif

// Standard Input/Output functions
#include <stdio.h>

// Declare your global variables here
 
void main(void)
{
// Declare your local variables here
  int weldisp=0;
  char rcv;
  int temp2;
  char temp;
  int ring;
  int i;
  int check;
  int check1;
  int check2;
  int check3;
  int cliplength;
  int call=1;
  int timer;
  char tel1[10]={0x36,0x39,0x37,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
  char tel2[10];
  char clip[12];
  int length;
  int call_count=0;
  int call_success=0;
  
  
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
 
PORTA=0x00;
DDRA=0x04;
PORTB=0x00;
DDRB=0xFF;
PORTD=0x0D;
DDRD=0x32;
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-10: Off
// Interrupt on any change on pins PCINT11-17: Off
MCUCR=0x02;
GIMSK=0x40;
GIFR=0x40;
TIMSK=0x00;
USICR=0x00;
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x06;
UBRRH=0x00;
UBRRL=0x67;
ACSR=0x80;
DIDR=0x00;

// I2C Bus initialization
// I2C Port: PORTB
// I2C SDA bit: 0
// I2C SCL bit: 1
// Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu.
i2c_init();

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 4
// RD - PORTD Bit 6
// EN - PORTB Bit 6
// D4 - PORTB Bit 2
// D5 - PORTB Bit 3
// D6 - PORTB Bit 4
// D7 - PORTB Bit 5
// Characters/line: 20
lcd_init(20);



// Global enable interrupts
#asm("sei")

 while (1){
    if (weldisp==0) { 
   
    PORTB.7=1;
  lcd_clear();
  lcd_gotoxy(7,0);                         	
  lcd_putsf("Welcome");           			
  lcd_gotoxy(5,2);                         	
  lcd_putsf("Version 3.5");           		
  lcd_gotoxy(5,3);                        
  lcd_putsf("27/12/14");           			
  weldisp=1; 
  lcd_gotoxy(0,3); 
   flag_open=0x32;
   delay_ms(10);
   
  lcd_putchar(flag_open);
  delay_ms(1000);
  lcd_gotoxy(1,3);
    flag_open=0x31; 
    
    delay_ms(10);    
    lcd_putchar(flag_open);
  delay_ms(1000);
  lcd_clear(); 
  PORTD.5=1;
    for(i=1;i<10;i++){
        delay_ms(250);
        #asm("wdr");
     } 
   for(i=0;i<=9;i++){
      i2c_start();
       i2c_write(0x48);           		//HEX48 ADDRES
       i2c_write(0x00);            		//XTAL = ON
        //i2c_write(0x30+clip[i]);   	        //ADD 30HEX TO ARRAY clip[i]=ASCII 
        // i2c_write(clip[i]+0x30);   	        //ADD 30HEX TO ARRAY clip[i]=ASCII
        i2c_write(tel1[i]&0x1F);
        lcd_putchar(tel1[i]);
        #asm("wdr");
        delay_ms(200);             		//DTMF MARK TIME
        i2c_write(0x01);           		// XTAL = OFF
        i2c_stop(); 
        #asm("wdr");
       
        delay_ms(150);
        #asm("wdr");
     } 
     for(i=1;i<20;i++){
        delay_ms(500);
        #asm("wdr");
     } 
     PORTD.5=0;
     PORTB.7=0;
  }
  #asm("wdr");
  
     lcd_clear();
  lcd_gotoxy(5,0);
  lcd_putsf("standby");
  while(PIND.3==1&&call==1){
   
  if(flag_open==0x38){
    while(call_count<5&&call_success==0){
     int i;
    char tel[10]={0x32,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
     
    delay_ms(10);
    PORTD.5=1;
    
   for(i=0;i<=9;i++){
      i2c_start();
       i2c_write(0x48);              //HEX48 ADDRES
       i2c_write(0x00);              //XTAL = ON
        //i2c_write(0x30+clip[i]);   //ADD 30HEX TO ARRAY clip[i]=ASCII 
        // i2c_write(clip[i]+0x30);  //ADD 30HEX TO ARRAY clip[i]=ASCII
        i2c_write(tel[i]&0x1F);
        lcd_gotoxy(i,0);
        lcd_putchar(tel[i]);
        #asm("wdr");
        delay_ms(200);               //DTMF MARK TIME
        i2c_write(0x01);             // XTAL = OFF
        i2c_stop(); 
        #asm("wdr");
       
        delay_ms(150);
        #asm("wdr");
     } 
     for(i=1;i<20;i++){
        delay_ms(500);
        #asm("wdr");
     }
     while(PORTD.6==0){
        
     } 
     PORTD.5=0;
     call_success=1;
  
   }
  }
  if(PIND.3==0){
   delay_ms(200);
   if(PIND.3==0){
    call=0;
    temp=0;
    temp2=0;
   }
  } 
  
  }
  while(call==0){
   while(PIND.3==0&&temp==0){
    temp=0;
    ring=1;
    call=0;
    lcd_clear();
    lcd_putsf("incoming call");
    #asm("wdr");
    }
   if(PIND.3==1&&temp==0&&ring==1){
    lcd_clear();
    temp=2;
    temp2=0;
    PORTB.7=0;
    #asm("wdr");
    } 
           
   if(PIND.3==1&&temp==2){
   PORTB.7=0;
     check=0;
     check1=0;
     check2=0;
     check3=0;
     
   
   check=0;
   //delay_ms(100);
   lcd_clear();
   
   #asm("wdr");                 
   i=0; 
    while(rx_counter==0){
        #asm("wdr");
        if(rx_counter>0) break;
    }
    while(PIND.3==1&&temp<=4){   
     rcv=getchar();
      #asm("wdr");  
     
      
        
        temp++;
        check=1;
     } 
      
     
       
       temp++ ;
       check1=1;
    
     
      
    }
     PORTB.7=1; 
     //delay_ms(2000);
     
     temp=10;
   }
   
     
   if(temp==10) {
    temp=1;
    ring=0;
    call=1;
    //delay_ms(2000);  
   } 
   }
   timer=5;
   while(timer>0 ){
     while(PIND.3==0){
        delay_ms(20);
        timer++;
        #asm("wdr");
     }
    
    delay_ms(100);
     #asm("wdr");
      timer--;
     
    }                 
    
     for(i=0;i<4;i++){
       #asm("wdr");
       delay_ms(250);
    }
     lcd_gotoxy(0,3);
     lcd_putsf("loop open");
    PORTD.5=1;
    
    for(i=0;i<10;i++){
       #asm("wdr");
       delay_ms(250);
    }
    lcd_clear();
    //length=disp[0];
     for(i=1;i<=cliplength;i++){
      i2c_start();
       i2c_write(0x48);           		//HEX48 ADDRES
       i2c_write(0x00);            		//XTAL = ON
        //i2c_write(0x30+clip[i]);   	
        // i2c_write(clip[i]+0x30);   	
        i2c_write(clip[i]&0x1F);
        lcd_putchar(clip[i]);
        #asm("wdr");
        delay_ms(200);             		//DTMF MARK TIME
        i2c_write(0x01);           		// XTAL = OFF
        i2c_stop(); 
        #asm("wdr");
       
        delay_ms(150);
        #asm("wdr");
     } 
     
     for(i=1;i<6;i++){
       #asm("wdr");
       delay_ms(1000);     				//delay 3sec
     }  
    
       i2c_start();
       i2c_write(0x48);           		//HEX48 ADDRES
       i2c_write(0x00);            		//XTAL = ON 
       i2c_write(flag_open&0x1F);
       delay_ms(200);
       i2c_write(0x01);           		// XTAL = OFF 
       delay_ms(150);
        i2c_write(0x00);            	        //XTAL = ON 
       i2c_write(0x31&0x1f);
       delay_ms(250); 
       i2c_write(0x01);           		// XTAL = OFF 
       delay_ms(150);
        i2c_write(0x00);            	        //XTAL = ON 
       i2c_write(0x32&0x1f);
       delay_ms(250);
       i2c_write(0x01);           	        // XTAL = OFF 
       delay_ms(150);
        i2c_write(0x00);            	       //XTAL = ON
       i2c_write(0x33&0x1f);
       delay_ms(250);
       i2c_write(0x01);           	       // XTAL = OFF 
      i2c_stop();
      
     PORTD.5=0;  //loop control 
     PORTB.7=0;
     #asm("wdr");
     for(i=1;i<6;i++){
      delay_ms(250);
      #asm("wdr");
     }   
 }
}   

Thank You !

Last Edited: Mon. Jan 5, 2015 - 05:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am totally horrified by your 'structure' or rather lack of structure.

 

There seems to be no high level 'explanatory' comments.

Incidentally,   the CV libraries will kick the Watchdog during delay_ms() and getchar().    Look in the CV Help.

 

Sit down and draw your flowchart properly.   Boxes should be relatively high level.   e.g. "Send DTMF sequence"

You don't want to see 'primitive' statements in the flow chart main loop().

 

Of course,    the "Send DTMF sequence" would be another function (that may have its own flowchart)

 

David.

Last Edited: Mon. Jan 5, 2015 - 05:35 PM