atiny2313 - 테스트 소스.
#include <avr/io.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#define sbi(PORT,BIT) PORT|=_BV(BIT) //set bit
#define cbi(PORT,BIT) PORT&=~_BV(BIT) //clear bit
void delay_us(unsigned int delay) // 1us
{
register unsigned int i;
for(i=0;i<delay;i++)
{
asm volatile("PUSH R0");
asm volatile("POP R0");
asm volatile("PUSH R0");
asm volatile("POP R0");
}
}
void delay_ms(unsigned int time_ms) // 1ms
{
register unsigned int i;
for(i=0;i<time_ms;i++)
{
delay_us(250);
delay_us(250);
delay_us(250);
delay_us(250);
}
}
/********************************************************************************************
string 처리 함수들
*******************************************************************************************/
void __strcpy(char* str1,char* str2)
{
int i = 0;
while(*(str2+i)!= 0) {
*(str1+i) = *(str1+2);
i++;
}
*(str1+i) = '\0';
}
// str : string
// return : size of str
char __strlen(char *str) // verified
{
char ret = 0;
while( (*(str+ret)) != '\0') {
ret++;
if (ret > 100) {
break;
}
}
return ret;
}
void __strcatc(char* str,char c)
{
char j;
j = __strlen(str);
*(str+j) = c;
*(str+j+1) = '\0';
}
void __clrstr(char* str,char size)
{
int i;
for(i = 0; i< size;i++) *(str+i) = '\0';
}
unsigned int __atoi(char* str) // verified
{
char i;
unsigned c;
unsigned int ret =0;
char s = __strlen(str);
for(i=0;i < s;i++) {
c = (*(str + i)) - '0';
ret = ret*10 + c;
}
return ret;
}
void __itoa(char* str,char s,long i) // verified
{
char c;
*(str+s -1) = '\0';
if(i < 0) {
*str = '-';
i = i * -1;
} else {
*str = '+';
}
for(c = s - 2 ;c >= 1;c--) {
*(str+c) = i % 10 + '0';
i = i / 10;
}
}
void __putch(char data)
{
while(!(UCSRA &0X20));
UDR=data;
UCSRA |= 0X20;
}
void send_hex(char data)
{
__putch(data/10 + '0');
__putch(data%10 + '0');
}
void __puts(char* str)
{
do{
__putch(*(str));
str++;
} while(*(str) != '\0');
}
volatile short angle[8] = {0,0,0,0,0,0,0,0};
volatile unsigned char buff[16];
volatile unsigned char buff_offset = 0;
#define DELAY_TIME 2
#define PULSE_WIDTH 50
// 0xffff - 625 = 0xfd82
#define FREQ_TIME 0xfd82
#define MAX_SERVO_VAL 99
SIGNAL(SIG_TIMER1_OVF)
{
asm volatile("cli"); // disable interrupt
int i;
TCNT1=FREQ_TIME;
__putch('s');
PORTB = 0xff;
delay_us(220);
for(i = 0 ;i < 254;i++) {
delay_us(2);
if(i == angle[0]) {
cbi(PORTB,1);
}
if(i == angle[1]) {
cbi(PORTB,2);
}
if(i == angle[2]) {
cbi(PORTB,3);
}
if(i == angle[3]) {
cbi(PORTB,4);
}
}
asm volatile("sei"); // disable interrupt
}
SIGNAL(SIG_USART0_RX )
{
asm volatile("cli"); // disable interrupt
unsigned char c;
c=UDR;
UCSRA |= 0X80;
*(buff + buff_offset) = c;
if(c == 0) {
buff_offset = 0;
angle[0] = buff[0];
angle[1] = buff[1];
angle[2] = buff[2];
angle[3] = buff[3];
angle[4] = buff[4];
angle[5] = buff[5];
angle[6] = buff[6];
angle[7] = buff[7];
}
asm volatile("sei"); // enable interrupt
}
int main()
{
unsigned char c;
// unsigned char buff[30];
unsigned char offset;
asm volatile("cli"); // disable interrupt
DDRB = 0xff; // 1111 1111
DDRD = 0xFE;
// serial set
UCSRA=0X00;
UCSRB=0X98; // (1<<RXEN) | (1<<TXEN); // recieve enalbe, trnas enable..
UCSRC= 0X06; //(1<<USBS) | (3 << UCSZ0); // async, no parity, 1 stop bit
// BAUD = 16M / 16 /(UBRR+1)
// UBRR = 20M / 16 / BAUD - 1
UBRRH=0X00;
UBRRL=0X67; //9600 bps
PORTB = 0x00;
TCCR1B |= 0X05; // pre-scaler : clk/ 1024 = 31250 hz
// 31250 / 625 = 50Hz (20ms)
TCNT1=FREQ_TIME;
// OCR1A=0X001;
TIMSK=(1<<TOIE1); // TOIE1에 인터럽트허용
angle[0] = angle[1] = angle[2] = angle[3] = 120;
asm volatile("sei"); // enable interrupt
c = 'f';
offset = 0;
do {
} while(1);
return 1;
}