BASIC4MCU | 질문게시판 | 답변 : Atmega128 서로 간섭받지 않는 두 코드를 붙이고 싶습니다..
페이지 정보
작성자 master 작성일2018-12-09 11:30 조회6,170회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6594
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-09 오전 11:35:26// by Ok-Hyun Park//#define F_CPU 16000000#include <avr/io.h>#include <util/delay.h>#include <stdio.h>#include "UART1.h"#include <avr/interrupt.h>//#define ROTATION_DELAY 1000 // 1초 대기#define PULSE_MIN 1087 // 최소 펄스 길이#define PULSE_FIRSTMID 2324 // 60도의 펄스 길이//#define FUNCSET 0x28#define ENTMODE 0x06#define ALLCLR 0x01#define DISPON 0x0c#define LINE2 0xC0#define LINE3 0x94#define LINE4 0xD4#define PORT_DATA PORTC#define DDR_DATA DDRC#define PIR_sensor PA0 // 적외선 센서#define ROTATION_DELAY 1000 // 1초 대기#define PULSE_MIN 1087 // 최소 펄스 길이#define PULSE_SECOND 4648 // 60도의 펄스 길이//FILE OUTPUT \=FDEV_SETUP_STREAM(UART1_transmit,NULL,_FDEV_SETUP_WRITE);FILE INPUT \=FDEV_SETUP_STREAM(NULL,UART1_receive,_FDEV_SETUP_READ);uint8_t keystate[4][4];//volatile int state=0;//unsigned int PIR_Status=0;//void InitializeTimer1(void){TCCR1A|=(1<<WGM11); TCCR1A|=(1<<COM1A1); TCCR1A|=(1<<COM1B1);TCCR1B|=(1<<WGM12)|(1<<WGM13); TCCR1B|=(1<<CS11);ICR1=39999;}//void LCD_write_command(unsigned char byte){_delay_ms(2);PORT_DATA=(byte&0xF0); PORT_DATA&=0xFE; PORT_DATA&=0xFD; _delay_us(1); PORT_DATA|=0x04; _delay_us(1); PORT_DATA&=0xFB;PORT_DATA=((byte<<4)&0xF0); PORT_DATA&=0xFE; PORT_DATA&=0xFD; _delay_us(1); PORT_DATA|=0x04; _delay_us(1); PORT_DATA&=0xFB;}//void LCD_write_data(unsigned char byte){_delay_ms(2);PORT_DATA=(byte&0xF0); PORT_DATA|=0x01; PORT_DATA&=0xFD; _delay_us(1); PORT_DATA|=0x04; _delay_us(1); PORT_DATA&=0xFB;PORT_DATA=((byte<<4)&0xF0); PORT_DATA|=0x01; PORT_DATA&=0xFD; _delay_us(1); PORT_DATA|=0x04; _delay_us(1); PORT_DATA&=0xFB;}//void LCD_write_string(char*string){uint8_t i; for(i=0; string[i]; i++){ LCD_write_data(string[i]); }}//void LCD_init(void){DDR_DATA=0xFF; PORT_DATA&=0xFB; _delay_ms(15);LCD_write_command(0x20); _delay_ms(5);LCD_write_command(0x20); _delay_us(100);LCD_write_command(0x20);LCD_write_command(FUNCSET); LCD_write_command(DISPON); LCD_write_command(ALLCLR); LCD_write_command(ENTMODE);}//void INIT_PORT(){DDRB=0x0F; PORTB=0x00;DDRD=0x00; PORTD=0x0F;}//void INIT_INT0_INT1_INT2_INT3(){EIMSK|=(1<<INT0)|(1<<INT1)|(1<<INT2)|(1<<INT3);EICRA|=(1<<ISC01)|(1<<ISC11)|(1<<ISC21)|(1<<ISC31);sei();}//void InitializeTimer3(void){ //3번 타이머/카운터의 고속PWM 모드를 이용한다.TCCR3A|=(1<<WGM31); TCCR3A|=(1<<COM3A1);TCCR3B|=(1<<WGM32)|(1<<WGM33); TCCR3B|=(1<<CS31); //분주율 8ICR3=39999; //20ms주기}//void read_key(){for(int x=0; x<4; x++){PORTB|=0x0F; PORTB&=~(0x01<<x); _delay_ms(10);uint8_t read=PIND;for(int y=0; y<4; y++){if(bit_is_set(read,y)){ keystate[x][y]=0; }else { keystate[x][y]=1; }}_delay_ms(300);}}//int ebs(int password[4][4],int pad[4][4]){ //일치 확인 함수for(int i=0; i<4; i++){for(int g=0; g<4; g++){if(password[i][g]==pad[i][g]){ return 1; } // password와 pad가 일치할때 1 반환}}}//void print_key(){ //푸티에서 평상시.,버튼 누를시0으로 나오도록 출력for(int x=0; x<4; x++){for(int y=0; y<4; y++){ printf("%c ",(keystate[y][x]?'0': '.')); }printf("\r\n");}printf("\r\n\r\n"); _delay_ms(300);}//int saveValue(int x,int y){if (x==0&&y==0)return 1;else if(x==1&&y==1)return 5;else if(x==2&&y==2)return 9;else if(x==2&&y==1)return 6;}//ISR(INT0_vect){ state=1; }ISR(INT1_vect){ state=1; }ISR(INT2_vect){ state=1; }ISR(INT3_vect){ state=1; }//int main(void){int password[4]={ 1,5,9,6}; // 암호 369#설정int pad[4]={0,}; //입력이 될 패드int index=0,count=0,value=0;//stdout=&OUTPUT;stdin=&INPUT;INIT_PORT();INIT_INT0_INT1_INT2_INT3();DDRE|=(1<<PE3); //모터 제어 핀을 출력으로 설정UART1_init();InitializeTimer3(); //3번 타이머/카운터 설정//DDRA=(0<<PIR_sensor);DDRB|=(1<<PB5)|(1<<PB6);InitializeTimer1();while(1){if(state==1){ read_key(); print_key(); state=0; }//for(int i=0; i<4; i++){for(int j=0; j<4; j++){if(keystate[i][j]==1){ pad[index]=saveValue(i,j); index++; }}}for(int i=0; i<4; i++){if(password[i]==pad[i])count++;printf("%d,\r\n",pad[i]); printf("count:%d \r\n",count);if(count==4)value=count;}count=0;if(value==4){OCR3A=PULSE_MIN; _delay_ms(ROTATION_DELAY); // 0도OCR3A=PULSE_FIRSTMID; _delay_ms(ROTATION_DELAY); // 60도//-----------------------------------------------------------2번 코드입니다.while(((PINA)&(1<<PIR_sensor))==1){LCD_init(); LCD_write_string("Sleeping"); _delay_ms(500);}LCD_write_command(ALLCLR);while(((PINA)&(1<<PIR_sensor))==0){LCD_init(); LCD_write_string("Hello Bro");OCR1A=PULSE_MIN; OCR1B=PULSE_MIN; _delay_ms(ROTATION_DELAY); // 0도OCR1A=PULSE_SECOND; OCR1B=PULSE_SECOND; _delay_ms(3000); // 120도}LCD_write_command(ALLCLR);//-----------------------------------------------------------2번 코드입니다.break;}}return 0;}
댓글 0
조회수 6,170등록된 댓글이 없습니다.