[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] ATMega128 TWI problems
From: |
Nick Brent |
Subject: |
[avr-gcc-list] ATMega128 TWI problems |
Date: |
Sun, 30 May 2004 10:35:28 -0700 |
Hey all,
I've been banging my head against this problem for two days now, and I
figure I need to get some outside help. The code below is a dumbed down version
of some code I wrote to interface with the ATMega128 TWI (or I2C) peripheral.
At first the problem I thought I was having was that the interrupt handler
wasn't picking up control when it was called, but now I'm not sure what the
problem is exactly.
What happens when I run this code on my chip is that the internal pull-ups
are enabled and the line goes high (which should happen), "Ready..." is
displayed on the LCD (which should happen), The LCD is cleared and the SDA line
goes low (which should happen when sending a START signal), but then nothing
happens after that. The 'dubug' sub-function is called in both cases where the
interrupt is executed or not, but nothing ever shows up on the LCD. I know the
problem isn't in my LCD or dubug code because I've used these subfuctions in
many applications without a problem.
Any input anyone could give me would be very appreciated. The code is for
my robot which I'm trying to get ready for PDXBot.04 mini-sumo competition next
weekend, so I'm kind of in a crunch. Thank you very much in advanced.
Sincerely,
-Chris Troutner
-------------------------->Begin Code<---------------------
/*********************************************
Project : srf085.c
Version : 1.0
Date : 5/30/2004
Author : Chris Troutner
Comments: This program is a dummbed down version of SRF083.c.
It's an attempt to get the TWI inteurrpt to fire.
Robot Type: : ModelT
*********************************************/
#include <modelt.h>
#include <avr/interrupt.h>
char t[16];
void dubug(uint16_t level);
void dubug(uint16_t level) {
asm volatile ("cli"); //Turn off all interrupt sources
clr_lcd();
// strcpy(t,"test");
sprintf(t,"Er: %d ",level); //Write the error level value to 't'
line1(t); //Display 't' on the LCD
while (1) {}; //loop forever (halt code execution)
}
INTERRUPT(SIG_2WIRE_SERIAL)
{
DDRB=0x0F; //set lower 4 bits or portB to output (LED's)
PORTB=0x0F; //Drive lower 4 bits of PORTB High (Turn on LED's)
TWCR = (TWCR | BV(TWINT)); //write a logic 1 to TWINT bit to remove
inturrupt flag
dubug(4); //disply 'Er: 4' on LCD and wait forever.
}
int main(void)
{
// Declare your local variables here
init();
DDRD=0x00; //set portD to all input
PORTD=0x02; //Turn on internal pullups on PORTD
lcd_init();
//Turn on LCD so I know SOMETHING is working
strcpy(t,"Ready...");
line1(t);
ms_spin(1000);
clr_lcd();
/*------->START TWI Initialization<----------*/
/* set the I2C bit rate generator to 100 kb/s (see pg. 202 & 206 in Mega128
datasheet)*/
TWSR = TWSR & 0xFC; //TWPS = 0
TWBR = 18; //TWBR = 18
//SCL Frequency = (CPU Clock Frequency)/(16+2(TWBR)*4^TWPS)
//CPU Clock Frequency = 16Mhz for MyRobot Brainboard
/* Enable Two Wire Interface*/
TWCR |= BV(TWEN);
TWCR |= BV(TWINT); //write a logic 1 to TWINT bit to make sure inturrupt
flag is not set
// enable TWI interrupt and slave address ACK
sbi(TWCR, TWIE);
sbi(TWCR, TWEA);
/*------->END TWI Initialization<--------*/
/* enable interrupts */
asm volatile ("sei");
TWCR = TWCR | (BV(TWINT)|BV(TWSTA)|BV(TWEN)); //send START bit. This should
generate an interrupt
ms_spin(1000); //wait to give interrupt a chance to fire
dubug(2); //send 'Er: 2' to LCD if interrupt doesn't work.
while (1) {}; //loop forever. We shouldn't reach this point.
return (1);
}
<---------------------End Code------------------------------->
- [avr-gcc-list] ATMega128 TWI problems,
Nick Brent <=