我对微控制器的编程相当陌生。我对Arduino有一些经验,但是在我几乎完成了我的项目之后,我欺骗地把我目前的项目转移到更便宜和更小的地方。因此,我现在使用的AVR ATmega32与爱特梅尔工作室。
我试图使用ATmega32与MAX7219芯片进行通信,以便与led矩阵进行多路复用。然而,我确实有多个不同的设备,我想与之沟通。
如果不实际使用微控制器上提供的SPI引脚,我如何与设备通信?我做了一个测试项目,但似乎出了点问题,我不知道是什么问题。我想我成功地让它进入了测试模式,因为所有的LED都是亮的,但是我不能让它在那之后做任何事情。我甚至不能清除显示器/关闭它。我又检查了一遍电线。就引脚配置和分配引脚而言,我的编码是否不正确?对此有什么建议或更好的方法来编写我的代码?
下面是指向MA7219数据表的链接
//test
#include <avr/io.h>
int main(void)
{
DDRB = 0b00000111; // pin 1(data), 2(clock) and 3(latch) are outputs
PORTB = 0 << PINB0; // data pin 1 is low
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low
uint16_t data;
data = 0b0000110000000000; // data to shift out to the max7219
//read bit
uint16_t mask;
for (mask = 0b0000000000000001; mask>0; mask <<= 1)
{
//iterate through bit mask
if (data & mask)
{ // if bitwise AND resolves to true
// send one
PORTB = 1 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}
else{ //if bitwise and resolves to false
// send 0
// send one
PORTB = 0 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}
}
PORTB = 1 << PINB2; // latch all the data
PORTB = 1 << PINB0; // data pin 1 is high
PORTB = 0 << PINB1; // clock pin 2 is low
PORTB = 0 << PINB2; // latch pin 3 is low
}发布于 2014-06-04 04:25:37
是的,您的bit代码有一个问题,就是每次都分配寄存器的全部值,而不保留现有的值。因此,您擦除您的数据信号,在您驱动您的时钟,违反了保持时间的接收器,并导致不可预测的操作。
与其用=分配引脚,不如用|=设置引脚,或者用&= ~(value)清除引脚。
例如:
PORTB = 1 << PINB0; //drive data
// tick
PORTB |= 1 << PINB1; //clock high PRESERVING data
// tock
PORTB &= ~(1 << PINB1); //clock low您还可能需要在针操作之间插入一个轻微的延迟。
从技术上讲,考虑到您已经将if用于数据状态,您还可以在赋值中使用OR重新驱动数据信号,例如
if (data & mask)
{ // if bitwise AND resolves to true
// send one
PORTB = 1 << PINB0;
// tick
PORTB = (1 << PINB1) | (1 << PINB0);
// tock
PORTB = 0 << PINB1 | (1 << PINB0);
}
else{ //if bitwise and resolves to false
// send 0
// send one
PORTB = 0 << PINB0;
// tick
PORTB = 1 << PINB1;
// tock
PORTB = 0 << PINB1;
}https://stackoverflow.com/questions/24028931
复制相似问题