新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > AVR單片機atmega16串口中斷接收和發送程序

        AVR單片機atmega16串口中斷接收和發送程序

        作者: 時間:2016-11-13 來源:網絡 收藏
        // Target : M16

        // Crystal: 8.0000Mhz
        //AVR單片機atmega16串口中斷接收和發送程序。接收到字符后再發送出去
        #include
        #include
        unsigned char com_in_buf[1];
        void port_init(void)
        {
        PORTA = 0xFF;
        DDRA = 0xFF;
        PORTB = 0x00;
        DDRB = 0x00;
        PORTC = 0x00;
        DDRC = 0x00;
        PORTD = 0x00;
        DDRD = 0x00;
        }

        本文引用地址:http://www.104case.com/article/201611/316518.htm

        //UART0 initialize
        // desired baud rate: 9600
        // actual: baud rate:9615 (0.2%)
        // char size: 8 bit
        // parity: Disabled
        void uart0_init(void)
        {
        UCSRB = 0x00; //disable while setting baud rate
        UCSRA = 0x00;
        UCSRC = BIT(URSEL) | 0x06;
        UBRRL = 0x33; //set baud rate lo
        UBRRH = 0x00; //set baud rate hi
        UCSRB = 0x98;
        }

        void send_data(unsigned char back)
        {
        while( !( UCSRA & (1< UDR=back;
        }

        #pragma interrupt_handler uart0_rx_isr:12
        void uart0_rx_isr(void)
        {
        //uart has received a character in UDR
        com_in_buf[0]=UDR;
        send_data(com_in_buf[0]);
        }

        //call this routine to initialize all peripherals
        void init_devices(void)
        {
        //stop errant interrupts until set up
        CLI(); //disable all interrupts
        port_init();
        uart0_init();

        MCUCR = 0x00;
        GICR = 0x00;
        TIMSK = 0x00; //timer interrupt sources
        SEI(); //re-enable interrupts
        //all peripherals are now initialized
        }

        void main(void)
        {
        char j;
        init_devices();
        while(1)
        {
        for(j=1;j<100;j++)
        ;
        }
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 汝城县| 屏南县| 颍上县| 澜沧| 桑日县| 云南省| 敖汉旗| 高州市| 潞城市| 贵溪市| 安乡县| 灌阳县| 昌江| 达拉特旗| 舟山市| 昌图县| 股票| 山东省| 武平县| 新巴尔虎右旗| 横峰县| 安平县| 泸西县| 原阳县| 容城县| 镇雄县| 安泽县| 九龙坡区| 吉林市| 奉节县| 精河县| 浠水县| 武功县| 汾阳市| 淮北市| 平湖市| 寻乌县| 商水县| 永州市| 潮州市| 枝江市|