共3条
1/1 1 跳转至页
I2C 帮看看这个I2C程序为什么不能启动啊?
问
//ICC-AVR application builder : 2004-3-6 15:15:32
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
unsigned char value,temp;
unsigned char i2c_sta=0x00;
//void error(err_no);
void port_init(void)
{
DDRB = 0b00011110;
DDRD = 0b00011111;
}
//TIMER1 initialisation - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x85; //setup
TCNT1L = 0xEE;
OCR1AH = 0x7A;
OCR1AL = 0x12;
OCR1BH = 0x7A;
OCR1BL = 0x12;
ICR1H = 0x7A;
ICR1L = 0x12;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x85; //reload counter high value
TCNT1L = 0xEE; //reload counter low value
value=(value<9)?(value+1):0;
temp=0x00|(value<<1);
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWIE);
PORTB=temp;
}
//TWI initialisation
// bit rate:200
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
}
#pragma interrupt_handler twi_isr:18
void twi_isr(void)
{
//twi event
i2c_sta=(i2c_sta^0xFF);
PORTD=i2c_sta;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
twi_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void main (void)
{
init_devices();
while(1);
}
我不知道这样启动I2C对不对,我用的是中断的方式。这个程序只作测试,要的是看看能不能进I2C的中断。但是就是进不去。请大家帮帮忙。 答 1: 写了一个更简单的,都还不行。#include <iom8v.h>
#include <macros.h>
unsigned char i2c_sta=0x00;
#pragma interrupt_handler twi_isr:18
void twi_isr(void)
{
//twi event
i2c_sta=(i2c_sta^0xFF);
PORTD=i2c_sta;
}
void main (void)
{
CLI();
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
SEI();
TWCR|=(1<<TWSTA)|(1<<TWIE);
while(1);
}
到底在干什么啊??我只求能进中断。 答 2: 好像没有启动哦1void main (void)
{
CLI();
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
SEI();
TWCR|=(1<<TWSTA)|(1<<TWIE);///好像没有启动哦1<<TWEN
while(1);
}
答 3: 前面初始化的时候不是启动了一次吗?是每次都要这样启动吗? 答 4: 再批一下双龙那本《mega8原理及应用》为了硬件I2C,害我忙了一天!
翻译意思不够准确就算了,是和非,0和1这种错误可是工作不负责任啊
去年遇到的是PWM问题,也是0和1不分 答 5: 楼上的兄弟:你说的是什么地方啊?我可能就是卡在这些上面。 答 6: 看英文资料吧,不是很难的TWI就三个寄存器TWSR,TWCR,TWDR 答 7: 我就是看的英文资料啊。就是怕理解上有什么偏差。 答 8: 参考下吧,一个测试程序/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.1d Standard
Automatic Program Generator
?Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro
Project :
Version :
Date : 2004-3-5
Author : ly
Company :
Comments:
Chip type : ATmega128
Program type : Application
Clock frequency : 3.686400 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 1024
*****************************************************/
#include <mega128.h>
#include <delay.h>
// Declare your global variables here
unsigned char twibz=0,temp=0,data,num=0;
interrupt [TWI] void twi_isr(void)
{
twibz=1;
temp=TWSR;
temp&=0xf8;
switch(temp)
{
case 0x08:
TWDR=0x87;
TWCR=(1<<7)|(1<<2)|1;
break;
case 0x40:
TWCR=(1<<7)|(1<<2)|1;
break;
case 0x58:
twibz=0;
data=TWDR;
TWCR=(1<<7)|(1<<4)|(1<<2)|1;
break;
case 0x48:
// TWCR=(1<<7)|(1<<5)|(1<<2)|1;
num++;
// twibz=0;
// TWCR=(1<<7)|(1<<4)|(1<<2)|1;
TWCR=(1<<7)|(1<<5)|(1<<2)|1;
break;
case 0x10:
TWDR=0x87;
TWCR=(1<<7)|(1<<2)|1;
break;
default:
PORTA=TWSR;
break;
}
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xff;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;
// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise CANceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: Off
// 2 Wire Bus Slave Address: 40h
// General Call Recognition: Off
// Bit Rate: 7.008 kHz
TWBR=0xFF;
TWAR=0x80;
TWCR=0x05;
#asm("sei");
while (1)
{
// Place your code here
if(twibz==0)
{
TWCR=(1<<7)|(1<<5)|(1<<2)|1;
}
// PORTA=num;
}
}
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
unsigned char value,temp;
unsigned char i2c_sta=0x00;
//void error(err_no);
void port_init(void)
{
DDRB = 0b00011110;
DDRD = 0b00011111;
}
//TIMER1 initialisation - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: 1.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x85; //setup
TCNT1L = 0xEE;
OCR1AH = 0x7A;
OCR1AL = 0x12;
OCR1BH = 0x7A;
OCR1BL = 0x12;
ICR1H = 0x7A;
ICR1L = 0x12;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x85; //reload counter high value
TCNT1L = 0xEE; //reload counter low value
value=(value<9)?(value+1):0;
temp=0x00|(value<<1);
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWIE);
PORTB=temp;
}
//TWI initialisation
// bit rate:200
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
}
#pragma interrupt_handler twi_isr:18
void twi_isr(void)
{
//twi event
i2c_sta=(i2c_sta^0xFF);
PORTD=i2c_sta;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
twi_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
void main (void)
{
init_devices();
while(1);
}
我不知道这样启动I2C对不对,我用的是中断的方式。这个程序只作测试,要的是看看能不能进I2C的中断。但是就是进不去。请大家帮帮忙。 答 1: 写了一个更简单的,都还不行。#include <iom8v.h>
#include <macros.h>
unsigned char i2c_sta=0x00;
#pragma interrupt_handler twi_isr:18
void twi_isr(void)
{
//twi event
i2c_sta=(i2c_sta^0xFF);
PORTD=i2c_sta;
}
void main (void)
{
CLI();
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
SEI();
TWCR|=(1<<TWSTA)|(1<<TWIE);
while(1);
}
到底在干什么啊??我只求能进中断。 答 2: 好像没有启动哦1void main (void)
{
CLI();
TWCR= 0X00; //disable twi
TWBR= 0xC8; //set bit rate
TWSR= 0x02; //set prescale
TWAR= 0x02; //set slave address
TWCR= 0x45; //enable twi
SEI();
TWCR|=(1<<TWSTA)|(1<<TWIE);///好像没有启动哦1<<TWEN
while(1);
}
答 3: 前面初始化的时候不是启动了一次吗?是每次都要这样启动吗? 答 4: 再批一下双龙那本《mega8原理及应用》为了硬件I2C,害我忙了一天!
翻译意思不够准确就算了,是和非,0和1这种错误可是工作不负责任啊
去年遇到的是PWM问题,也是0和1不分 答 5: 楼上的兄弟:你说的是什么地方啊?我可能就是卡在这些上面。 答 6: 看英文资料吧,不是很难的TWI就三个寄存器TWSR,TWCR,TWDR 答 7: 我就是看的英文资料啊。就是怕理解上有什么偏差。 答 8: 参考下吧,一个测试程序/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.1d Standard
Automatic Program Generator
?Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro
Project :
Version :
Date : 2004-3-5
Author : ly
Company :
Comments:
Chip type : ATmega128
Program type : Application
Clock frequency : 3.686400 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 1024
*****************************************************/
#include <mega128.h>
#include <delay.h>
// Declare your global variables here
unsigned char twibz=0,temp=0,data,num=0;
interrupt [TWI] void twi_isr(void)
{
twibz=1;
temp=TWSR;
temp&=0xf8;
switch(temp)
{
case 0x08:
TWDR=0x87;
TWCR=(1<<7)|(1<<2)|1;
break;
case 0x40:
TWCR=(1<<7)|(1<<2)|1;
break;
case 0x58:
twibz=0;
data=TWDR;
TWCR=(1<<7)|(1<<4)|(1<<2)|1;
break;
case 0x48:
// TWCR=(1<<7)|(1<<5)|(1<<2)|1;
num++;
// twibz=0;
// TWCR=(1<<7)|(1<<4)|(1<<2)|1;
TWCR=(1<<7)|(1<<5)|(1<<2)|1;
break;
case 0x10:
TWDR=0x87;
TWCR=(1<<7)|(1<<2)|1;
break;
default:
PORTA=TWSR;
break;
}
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xff;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;
// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;
// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise CANceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: Off
// 2 Wire Bus Slave Address: 40h
// General Call Recognition: Off
// Bit Rate: 7.008 kHz
TWBR=0xFF;
TWAR=0x80;
TWCR=0x05;
#asm("sei");
while (1)
{
// Place your code here
if(twibz==0)
{
TWCR=(1<<7)|(1<<5)|(1<<2)|1;
}
// PORTA=num;
}
}
共3条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
【换取逻辑分析仪】-基于ADI单片机MAX78000的简易MP3音乐播放器被打赏48分 | |
我想要一部加热台+树莓派PICO驱动AHT10被打赏38分 | |
【换取逻辑分析仪】-硬件SPI驱动OLED屏幕被打赏36分 | |
换逻辑分析仪+上下拉与多路选择器被打赏29分 | |
Let'sdo第3期任务合集被打赏50分 | |
换逻辑分析仪+Verilog三态门被打赏27分 | |
换逻辑分析仪+Verilog多输出门被打赏24分 | |
【分享评测,赢取加热台】使用8051单片机驱动WS2812被打赏40分 | |
【换取逻辑分析仪】rtthread添加RRH62000传感器驱动-基于野火启明6M5被打赏48分 | |
换逻辑分析仪+Verilog多输入门被打赏27分 |