라벨이 ATmega328PB인 게시물 표시

ATmega328PB i2c master mode example source

ATmega328PB에서 마스터 모드로 동작하는 i2c 예제 소스다. i2c led 컨트롤용으로 만들었던 코드다. 급하게 만들어서 read 함수가 없다. 컨트롤용이라 write만... datasheet를 보고 만들었다. 지금은 보드가 없어 read 함수 구현 테스트가 힘들다. 보드 생기면 구현해서 업데이트하자. iic.h #ifndef IIC_H_ #define IIC_H_ void iic_init(int ch); int write_iic(int ch, int address, int *data, int len); #endif /* IIC_H_ */ iic.c #define F_CPU 8000000UL // //#define F_CPU 16000000UL // #include <math.h> #include <stdlib.h> #include <inttypes.h> #include <avr/io.h> #include <avr/interrupt.h> #include <util/twi.h> #include <util/delay.h> #include "iic.h"   #ifndef TWI_FREQ #define TWI_FREQ 100000L #endif #define iic_timeout_ms 100 void iic_init(int ch) { if(ch == 0) { TWSR0 &= ~(1<<TWPS0); TWSR0 &= ~(1<<TWPS1); TWBR0 = ((F_CPU / ...

ATmega328PB USART example code

이미지
본 글은 ATmega328PB USART 드라이버 소스를 싣고 있다. uart를 이용한 디버그 메세지 출력이나, 다른 디바이스와의 통신에도 사용할 수 있다. USART_Init을 호출하여 baudrate를 설정한 후, USART_Read,USART_Write,USART_Rx, USART_Tx의 함수를 사용하여 데이터를 읽고 쓰면된다. [소스 코드] #define F_CPU 8000000UL //#define F_CPU 16000000UL  #include <math.h> #include <stdlib.h> #include <inttypes.h> #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #define usart_waittime_ms 100 void USART_Init(unsigned int baudrate) { /**/ unsigned int ubrr = (((F_CPU / (baudrate * 16UL))) - 1); /*Set baud rate */ UBRR0H = (unsigned char)(ubrr>>8); UBRR0L = (unsigned char)ubrr; /*Enable receiver and transmitter */ UCSR0B = (1<<RXEN0)|(1<<TXEN0); /* Set frame format: 8data, 2stop bit */ UCSR0C = (1<<USBS0)|(3<...

ATmega328PB timer example code gettickcount

이미지
본 글은 ATmega328PB에서 인터럽트를 사용하여 시간 tick을 가져오는 gettickcount를 구현한 예제 소스를 싣고 있다. 원리는 간단하다. 10 ms마다 인터럽트를 발생시키고 그때마다 count를 증가시키다. 그리고 필요할 떄  gettickcount () 함수를 호출하여 시간 tick을 받아온다. 즉, 부팅 이후 흘러간 시간 tick을 가져오는 함수이다. 소스코드는 아래와 같다. main()함수 시작시 timer_init()을 하고, 그 후 필요할때  gettickcount ()를 호출하면 된다. #define F_CPU 8000000UL // //#define F_CPU 16000000UL // #include <math.h> #include <stdlib.h> #include <inttypes.h> #include <stdbool.h> #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #define SYS_TICK_UNIT_MS 10 static volatile unsigned long system_tick_count = 0; ISR(TIMER1_COMPA_vect) { /*10 ms*/ system_tick_count++; } unsigned long gettickcount() { return (system_tick_count*SYS_TICK_UNIT_MS); } void timer_init() { /* set up the system 100Hz timer  (OCR1A+1) = delay / (F_...