BASIC4MCU | 질문게시판 | Atmega128 CLCD와 블루투스 통신 문제
페이지 정보
작성자 fanfan 작성일2023-12-07 16:11 조회1,458회 댓글1건본문
#include <avr/io.h>
#define F_CPU 16000000UL
#define __DELAY_BACKWARD_COMPATIBLE__
#include <util/delay.h>
//
#define STATE_0 0
#define STATE_1 1
#define STATE_2 2
#define STATE_3 3
#define NULL 0x00
//
#define BIT4_LINE2_DOT58 0x28
#define DISPON_CUROFF_BLKOFF 0x0C
#define DISPOFF_CUROFF_BLKOFF 0x08
#define INC_NOSHIFT 0x06
#define DISPCLEAR 0x01
#define CUR1LINE 0x80
#define CUR2LINE 0xC0
#define CURHOME 0x02
#define E_BIT 0x04
#define RW_BIT 0x03
#define RS_BIT 0x02
//
#define STOP_SPEED 0
#define LOW_SPEED 77
#define MID_SPEED 153
#define HIGH_SPEED 230
#define MOTOR_CW 0xb0
#define MOTOR_CCW 0x70
#define MOTOR_BRAKE 0xd0
#define MOTOR_STOP 0x30
#define MOTOR_STANDBY 0x00
//
char Fan_name[]="Good Fan";
char START_LCD[]="Put the number!";
char STOP_SPEED1[]="Turn Off!";
char LOW_SPEED1[]="Speed State 1";
char MID_SPEED1[]="Speed State 2";
char HIGH_SPEED1[]="Speed State 3";
char ERROR[]="Put 1 or 2 or 3";
int motor_speed[4]={STOP_SPEED, LOW_SPEED, MID_SPEED, HIGH_SPEED};
unsigned char c=STATE_0;
//
void putchar1(char c){ while(!(UCSR1A & (1<<UDRE1))); UDR1=c; }
//
void puts1(char *ptr){ while(*ptr !=NULL)putchar1(*ptr++); }
//
char available1(){
if(UCSR1A & (1<<RXC1))return(1); else return(0);
}
//
char getchar1(){ while(!(UCSR1A & (1<<RXC1))); return(UDR1); }
//
void init_uart1(){
DDRE |= (1 <<PE0);
DDRE |= (1 << PE1);
UCSR1B=0x18;//(1 << RXEN0) | (1 << TXEN0); //0x18;/// -> 이 부분을 UCSR1B = (1 << RXEN1) | (1 << TXEN1); 으로
UCSR1C=0x06;//(1 << UCSZ11) | (1 << UCSZ10); //0x06;//// -> 이 부분을 UCSR1C = (1 << UCSZ11) | (1 << UCSZ10); 으로
UBRR1H=0;
UBRR1L=8;
}
//
void CLCD_data(char data){
PORTD=0x04; _delay_us(1); PORTD=0x14; PORTC=data & 0xf0;
PORTD=0x04; _delay_us(2); PORTD=0x14; PORTC=(data << 4) & 0xf0;
PORTD=0x04; _delay_ms(1);
}
//
void CLCD_cmd(char cmd){
PORTD=0x00; _delay_us(1); PORTD=0x10; PORTC=cmd & 0xf0;
PORTD=0x00; _delay_us(2); PORTD=0x10; PORTC=(cmd << 4) & 0xf0;
PORTD=0x00; _delay_ms(1);
}
//
void CLCD_puts(char *ptr){ while(*ptr !=NULL)CLCD_data(*ptr++); }
//
void init_CLCD(){
DDRC=0xff; DDRD=0xff;
_delay_ms(50);
CLCD_cmd(BIT4_LINE2_DOT58);
CLCD_cmd(DISPON_CUROFF_BLKOFF);
CLCD_cmd(INC_NOSHIFT);
CLCD_cmd(DISPCLEAR);
_delay_ms(2);
}
//
int main(void){
char c;
DDRB=0xf0; PORTB=MOTOR_STANDBY;
TCCR2=0x6b; OCR2=0;
init_CLCD();
_delay_ms(500);
init_uart1();
while(1){
if(!available1()){
CLCD_cmd(CUR1LINE);
CLCD_puts(Fan_name);
CLCD_cmd(CUR2LINE);
CLCD_puts(START_LCD);
}
c=getchar1();
putchar1(c);
switch(c){
case '0' :
CLCD_cmd(DISPCLEAR); CLCD_cmd(CUR1LINE); CLCD_puts(Fan_name);
CLCD_cmd(CUR2LINE); CLCD_puts(STOP_SPEED1);
PORTB=MOTOR_STOP; OCR2=motor_speed[0];
break;
case '1' :
CLCD_cmd(DISPCLEAR); CLCD_cmd(CUR1LINE); CLCD_puts(Fan_name);
CLCD_cmd(CUR2LINE); CLCD_puts(LOW_SPEED1);
PORTB=MOTOR_CW; OCR2=motor_speed[1];
break;
case '2' :
CLCD_cmd(DISPCLEAR); CLCD_cmd(CUR1LINE); CLCD_puts(Fan_name);
CLCD_cmd(CUR2LINE); CLCD_puts(MID_SPEED1);
PORTB=MOTOR_CW; OCR2=motor_speed[2];
break;
case '3' :
CLCD_cmd(DISPCLEAR);
CLCD_cmd(CUR1LINE);
CLCD_puts(Fan_name);
CLCD_cmd(CUR2LINE);
CLCD_puts(HIGH_SPEED1);
PORTB=MOTOR_CW; OCR2=motor_speed[3];
break;
default :
//CLCD_cmd(DISPCLEAR);
//CLCD_cmd(CUR1LINE);
//CLCD_puts(Fan_name);
//CLCD_cmd(CUR2LINE);
//CLCD_puts(ERROR);
break;
}
}
}
밑에 질문 했던 것처럼 블루투스 포트를 E 포트로 변경도 해보고,
블루투스 포트를 D 포트로 사용하면, LCD에서 사용하는 포트를 C로 전부 옮겨봤는데도 실패하였습니다...
그래서 블루투스 포트를 E 포트(PE0, PE1) (RXD0, TXD0)으로 하려고 하는데도 동작이 되질 않습니다...
블루투스를 E포트로 사용하려면 코드를 수정해야 하는지 도와주시면 감사하겠습니다..
댓글 1
조회수 1,458master님의 댓글
master 작성일
void init_uart1(){
UCSR1B=0x18; UBRR1L=8;
}
UART0을 사용하려면 UART1을 설정하지말고 UART0을 설정해야겠죠?
void init_uart0(){
UCSR0B=0x18; UBRR0L=8;
}
나머지 함수들도 UART0으로 변경합니다.
//
char available0(){ if(UCSR0A&0x80)return(1); else return(0); }
char getchar0(){ while(!(UCSR1A&0x80)); return(UDR0); }
void putchar0(char c){ while(!(UCSR0A&0x20)); UDR0=c; }
void puts0(char *s){ while(*s)putchar0(*s++); }