Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Last active January 25, 2019 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamiyaowl/2dfb3e788a5e9ec9ca6e to your computer and use it in GitHub Desktop.
Save kamiyaowl/2dfb3e788a5e9ec9ca6e to your computer and use it in GitHub Desktop.
dmx receive for avr
/*
* dmx_receiver.c
*
* Created: 2014/07/15 19:43:52
* Author: kamiya
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "dmx_receiver.h"
volatile uint8_t recv_data[DMX_CH];
volatile uint8_t data = 0;
volatile uint8_t is_fe = 0;
volatile uint16_t recv_counter = 0;
volatile uint8_t is_recv = 0;
//__ ______ ____
// |_____________| |[SC][ch1][ch2][ch3]...|
ISR(USART_RX_vect){
is_fe = (UCSR0A >> 4) & 0x1;
data = UDR0;
if(is_fe) recv_counter = 0x0;
else ++recv_counter;
if(1 < recv_counter && recv_counter < DMX_CH) recv_data[recv_counter - 2] = data;
is_recv = 0x1;
}
//////////////////////////////////////////////////////////////////////////
//dmx受信を初期化します
//sei()しないと受信を開始しないので注意してください
//内部でISR(USART_RX_vect)が定義されているので多重定義にも注意してください
void dmx_recv_init(){
//USART
UBRR0H = (MYUBRR >> 8) & 0xff;
UBRR0L = MYUBRR & 0xff;
UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
UCSR0C = (3<<UCSZ00) | (1 << 3);
data = 0x0;
is_fe = 0x0;
recv_counter = 0x0;
is_recv = 0x0;
dmx_recv_clear();
}
//////////////////////////////////////////////////////////////////////////
//受信データ配列を初期化します
void dmx_recv_clear(){
for(uint16_t i = 0 ; i < DMX_CH ; ++i){
recv_data[i] = 0x0;
}
}
//////////////////////////////////////////////////////////////////////////
//DMXを受信したかを返します。この関数を読み出し後受信フラグはリセットされます
uint8_t dmx_is_receive(){
uint8_t tmp = is_recv;
is_recv = 0x0;
return tmp;
}
/*
* dmx_receiver.h
*
* Created: 2014/07/15 19:43:41
* Author: kamiya
*/
#ifndef DMX_RECEIVER_H_
#define DMX_RECEIVER_H_
#define DMX_CH 128
#define BAUD 250000
#define MYUBRR (F_CPU/16/BAUD-1)
extern volatile uint8_t recv_data[DMX_CH];
void dmx_recv_init();
void dmx_recv_clear();
uint8_t dmx_is_receive();
#endif /* DMX_RECEIVER_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment