我尝试为ATTiny85实现只使用uart的TX,并使用arduino微接收比特。(相似问题没有帮助,因为它与我的情况完全不同)
我打算能够通过attiny85 -> arduino ->控制台打印,这样我就可以调试attiny85,因为我没有可用的示波器。
attiny85保险丝是:"efuse:w:0xff:m -U hfuse:w:0xdf:m -U lfuse:w:0xf1:m" aka。16 have F_CPU arduino似乎也有16 have F_CPU。
类似于上述问题,attiny85通过timer0 ISR同时发送比特:
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz
#endif
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define TX_PIN PB0
volatile uint16_t tx_shift_reg = 0;
ISR(TIMER0_COMPA_vect) {
uint16_t local_tx_shift_reg = tx_shift_reg;
if( local_tx_shift_reg & 0x01 ) {
PORTB |= _BV(TX_PIN);
} else {
PORTB &= ~_BV(TX_PIN);
}
local_tx_shift_reg >>= 1;
if(!local_tx_shift_reg) {
// Stop timer0.
GTCCR |= (1<<TSM) | (1<<PSR0);
}
tx_shift_reg = local_tx_shift_reg;
}
void UART_tx(char byte) {
uint16_t local_tx_shift_reg = tx_shift_reg;
local_tx_shift_reg = (0b1<<9) | ((uint16_t)byte<<1);
tx_shift_reg = local_tx_shift_reg;
TCNT0 = 0;
TCCR0B |= (1<<CS02)|(1<<CS00); // 1024 prescaler
GTCCR &= ~(1<<TSM);
}
void UART_tx_char(char c) {
UART_tx( c );
// wait until transmission is finished
while(tx_shift_reg);
}
void UART_init() {
cli()
// set TX pins as output
DDRB |= (1<<TX_PIN);
PORTB |= (1<<TX_PIN);
// set timer0 to CTC mode, keep it halted.
TCCR0A |= (1<<WGM01);
TCCR0B = 0;
// enable interrupt
TIMSK |= (1<<OCIE0A);
OCR0A = 128;
OCR0B = 128;
TCNT0 = 0;
GTCCR |= (1<<TSM);
sei();
}
void main(void)
{
UART_init();
while(1) {
for(char c = 1; c < 128; ++c) {
UART_tx_char(c);
_delay_ms(100);
}
}
}然后arduino接收到以下比特:
/*
* ATtiny85 bit-bang uart monitor for ATmega32u4
*/
#define LED_PIN 13
#define RX_PIN 7
// rx_state == 0 // timer1 not running
// rx_state == 1 // receive in progress
// rx_state == 2 // data ready in rx_data_reg
volatile int rx_state = 0;
volatile int rx_bit_nro = 0;
volatile uint16_t rx_shift_reg = 0;
volatile uint16_t rx_data_reg = 0;
void rx_start_interupt() {
if(rx_state == 0) {
digitalWrite(LED_PIN, HIGH);
while(digitalRead(RX_PIN));
// Start timer1
rx_state++;
TCNT1 = 0;
TCCR1B = (1 << WGM12) | (1 <<CS12) | (1 << CS10);
}
}
ISR(TIMER1_COMPA_vect) {
uint16_t bit = digitalRead(RX_PIN) > 0;
rx_shift_reg >>= 1;
rx_shift_reg |= (bit << 7);
++rx_bit_nro;
if(rx_bit_nro == 9) {
// Stop timer.
TCCR1B = 0;
TCNT1 = 0;
rx_data_reg = rx_shift_reg;
rx_shift_reg = 0;
rx_bit_nro = 0;
rx_state++;
digitalWrite(LED_PIN, LOW);
}
}
void setup() {
noInterrupts();
// Program timer1 for UART bit bang receive.
TCCR1A = 0; // set entire TCCR1A register to 0 (stops it)
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
OCR1A = 128;
TIMSK1 |= (1 << OCIE1A);
interrupts();
pinMode(LED_PIN, OUTPUT);
pinMode(RX_PIN, INPUT);
// Attach RX start interupt.
pinMode(digitalPinToInterrupt(RX_PIN), INPUT);
attachInterrupt(digitalPinToInterrupt(RX_PIN), rx_start_interupt, FALLING);
Serial.begin(9600);
while(!Serial);
Serial.print("F_CPU:");
Serial.println(F_CPU,DEC);
Serial.println("Waiting for data from attiny85...");
}
void loop() {
if(rx_state == 2) {
uint16_t local_rx_data = rx_data_reg;
rx_state = 0;
Serial.println(local_rx_data,HEX);
}
}我已经尽力让它工作了,但是接收到的字节返回garbace。我遗漏了什么?
注意:我在attiny85上使用attiny85,在arduino上使用timer1。解决了:将ISR切换到TIMER1_COMPAB_vect,OCR1B = 64实际工作!耶!
发布于 2021-02-09 10:46:53
最近,我遇到了另一个问题,就是串行通信以垃圾的形式出现。在我的例子中,ATtiny85使用UART转换器向我的笔记本电脑发送位数,这种转换器在其他情况下工作得很好,但我只是在Arduino IDE串行监视器中收到垃圾。
我发现了一个论坛帖子,它提到了校准OSCCAL的可能性。
在我的相关博客帖子中,我得到了一些爱好者,但我测试了应该使用以下代码校准OSCCAL的理论:
#include <SoftwareSerial.h>
SoftwareSerial comm(-1, 0);
static const int anchor = 128;
void
print_osccal(int v) {
comm.println(F("********************************"));
comm.print(F("OSCCAL = "));
comm.println(v);
comm.println(F("********************************"));
}
void
setup() {
delay(5000);
comm.begin(300);
OSCCAL = anchor;
print_osccal(anchor);
delay(5000);
}
void
loop() {
int x;
for (int i = 1; i < 128; ++i) {
x = anchor + i;
OSCCAL = x;
print_osccal(x);
delay(1000);
x = anchor - i;
OSCCAL = x;
print_osccal(x);
delay(1000);
}
}看到垃圾突然转变为串行监视器中格式良好的消息(当然,返回到垃圾,因为搜索是一个无限循环),这是一个启示。
现在,ATtiny85 85的内部振荡器只能支持1 MHz和8 MHz。据我所知,OSCCAL的存在是因为这个内部振荡器不太精确,而2)对温度非常敏感。
如果ATtiny85被设置为16 the运行,那么它需要一个可靠的外部振荡器,而任何与OSCCAL打交道的方法都不会有帮助。但是,在我的例子中,允许我发现使SoftwareSerial以8 MHz计算的值,以及从300到38400的波特率范围。
这使我能够获得正确的值,使位敲UART与1 1MHz时钟工作,这是不受SoftwareSerial支持的。
https://stackoverflow.com/questions/61090691
复制相似问题