JOYSTICK - PORT D
DC모터 - PORT C
→ 모터 출력핀의 HIGH, LOW 상태를 테스트하기 위해 모터 대신 LED를 PC2, PC3, PC4 핀에 연결
#include <avr/io.h>
#include <stdio.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#define F_CPU 3333333
#define BAUD_RATE 9600
#define _ADC0_CTRLA (unsigned char *)(0x0600)
#define _ADC0_CTRLA (unsigned char *)(0x0600)
#define _ADC0_MUXPOS (unsigned char *)(0x0606)
#define _ADC0_COMMAND (unsigned char *)(0x0608)
#define _ADC0_RES (unsigned char *)(0x0610)
#define _ADC_RESSEL_10BIT_gc (0x01<<0)
#define _ADC_ENABLE_bm (0x01)
#define _ADC_STCONV_bm (0x01)
#define PORTC (unsigned char *)(0x0440)
#define PORTC_DIR (unsigned char *)(0x0440)
#define PORTC_DIRSET (unsigned char *)(0x0441)
#define PORTC_DIRCLR (unsigned char *)(0x0442)
#define PORTC_OUT (unsigned char *)(0x0444)
#define PORTC_OUTSET (unsigned char *)(0x0445)
#define PORTC_OUTCLR (unsigned char *)(0x0446)
#define PORTC_IN (unsigned char *)(0x0448)
#define USART0_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
#define PIN(N) (1<<N)
void ADC_init(void);
uint16_t ADC_read(int ch);
void USART3_init(void);
void USART3_Transmit(unsigned char);
int USART3_printChar(char, FILE *stream);
FILE USART_stream = FDEV_SETUP_STREAM(USART3_printChar, NULL, _FDEV_SETUP_WRITE);
void USART3_init(void){
PORTB_DIR |= PIN(0);
PORTB_DIR &= ~PIN(1);
USART3_BAUD = (uint16_t)USART0_BAUD_RATE(BAUD_RATE);
USART3_CTRLB |= USART_TXEN_bm | USART_RXEN_bm;
}
int USART3_printChar(char c, FILE *stream){
USART3_Transmit(c);
return 0;
}
void USART3_Transmit(unsigned char data){
while(!((USART3_STATUS)&USART_DREIF_bm));
USART3_TXDATAL = data;
}
void ADC_init(void){
PORTD_PIN0CTRL = PORT_ISC_INPUT_DISABLE_gc;
ADC0_CTRLA = ADC_RESSEL_10BIT_gc;
ADC0_CTRLC |= ADC_REFSEL_VDDREF_gc;
ADC0_CTRLA |= ADC_ENABLE_bm | ADC_FREERUN_bm;
}
uint16_t ADC_read(int ch){
ADC0_MUXPOS = ch;
ADC0_COMMAND |= ADC_STCONV_bm;
while(!(ADC0_COMMAND) & ADC_STCONV_bm);
return (uint16_t)ADC0_RES;
}
int main(void)
{
USART3_init();
ADC_init();
stdout = &USART_stream;
*PORTC_DIRSET = PIN(2) | PIN(3) | PIN(4) | PIN(5);
int X_value = ADC_read(0);
_delay_ms(10);
int Y_value = ADC_read(1);
while (1)
{
int X_value = ADC_read(0);
_delay_ms(10);
int Y_value = ADC_read(1);
printf("X value : %d \\t Y value : %d \\n\\r", X_value, Y_value);
if(Y_value >= 505 && Y_value <= 521){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(2);
printf("stop\\n\\r");
_delay_ms(10);
}
else if(Y_value > 521){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(3);
printf("drive\\n\\r");
_delay_ms(10);
}
else if(Y_value <505){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(4);
printf("reverse\\n\\r");
_delay_ms(10);
}
_delay_ms(300);
}
}
*PORTC_DIRSET
*PORTC_DIRSET = PIN(2) | PIN(3) | PIN(4) | PIN(5);
→ PC2, PC3, PC4, PC5 핀을 출력핀으로 설정함.
조이스틱 값에 따른 출력핀 제어
if(Y_value >= 505 && Y_value <= 521){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(2);
printf("stop\\n\\r");
_delay_ms(10);
}
else if(Y_value > 521){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(3);
printf("drive\\n\\r");
_delay_ms(10);
}
else if(Y_value <505){
*PORTC_OUT = (*PORTC_OUT & ~(PIN(2) | PIN(3) | PIN(4))) | PIN(4);
printf("reverse\\n\\r");
_delay_ms(10);
}
Y값이 505 에서 521 사이
→ PC2, PC3, PC4 핀을 LOW 상태로 설정 후, PC2 핀을 HIGH 상태로 설정함.
→ “stop” 메시지 출력
Y값이 521 보다 클 때
→ PC2, PC3, PC4 핀을 LOW 상태로 설정 후, PC3 핀을 HIGH 상태로 설정함.
→ “drive” 메시지 출력
Y값이 505 보다 작을 때
→ PC2, PC3, PC4 핀을 LOW 상태로 설정 후, PC4 핀을 HIGH 상태로 설정함.
→ “reverse” 메시지 출력