Serial Communication Experiment Based on c8051f410

Source: Internet
Author: User

This sectionProgramThe function is very simple, that is, to send a specific command by pressing a button, and to control the light from the light when receiving the command,

But it is very valuable for debugging. I helped a member of the association call the communication between c8051f410 and AVR, and I had my own

Use it for some projects. Therefore, upload the file for your reference.

# Include <c8051f410. h> // SFr declarations # include <stdio. h> # define sysclk 24500000 // internal oscillator frequency in Hz # define baudrate 2400 // baud rate of UART in bpssbit led1 = P2 ^ 1; sbit led2 = P2 ^ 3; sbit key1 = p1 ^ 4; sbit key2 = p1 ^ 5; void oscillator_init (void); void port_init (void); void uart0_init (void); void uart_send (unsigned char m ); void delay (unsigned int m); void delay_ms (unsigned int N ); Void main (void) {pca0md & = ~ 0x40; // disable watchdog timer oscillator_init (); // initialize oscillator port_init (); // initialize crossbar and portsuart0_init (); EA = 1; // enable global interrupts while (1) {If (key1! = 1) {delay_ms (500); uart_send (0xb0); key1 = 1;} If (key2! = 1) {delay_ms (500); uart_send (0xb1); key2 = 1 ;}// wait for interrupt} void oscillator_init (void) {oscicn = 0x87; // set clock to 24.5 MHz rstsrc = 0x04; // enable missing clock detector} void port_init (void) {// p0mdin = 0xfc; // configure p0.0 and p0.1 to analog p0mdout | = 0x10; // enable utx as push-pull output p0skip = 0x03; // skip p0.0 and p0.1 on the crossbar p1mdin | = 0x30; p1mdout & = ~ 0x30; p1skip = 0x30; p2mdin = 0xff; p2mdout = 0x0a; p2skip = 0x0a; xbr0 = 0x01; // enable UART on limit 4 (TX) and between 5 (RX) xbr1 = 0x40; // enable crossbar and weak pull-ups} void uart0_init (void) {scon0 = 0x10; // scon0: 8-bit Variable Bit Rate // level of stop bit is ignored // RX enabled // ninth bits are zeros // clear ri0 and ti0 bits if (sysclk/baudrate/2/256 <1) {Th1 =-(sysclk/baudrate/2); ckcon | = 0 X08; // t1m = 1; SCA1: 0 = XX} else if (sysclk/baudrate/2/256 <4) {Th1 =-(sysclk/baudrate/2/4 ); ckcon & = ~ 0x0b; // t1m = 0; SCA1: 0 = 01 ckcon | = 0x01;} else if (sysclk/baudrate/2/256 <12) {Th1 =-(sysclk/baudrate/2/12); ckcon & = ~ 0x0b; // t1m = 0; SCA1: 0 = 00} else if (sysclk/baudrate/2/256 <48) {Th1 =-(sysclk/baudrate/2/48 ); ckcon & = ~ 0x0b; // t1m = 0; SCA1: 0 = 10 ckcon | = 0x02;} else {While (1); // error. unsupported baud rate} TL1 = Th1; // init timer1 tmod & = ~ 0xf0; // tmod: timer 1 in 8-bit autoreload tmod | = 0x20; tr1 = 1; // start timer1 IP | = 0x10; // make UART high priority es0 = 1; // enable uart0 interrupts} void uart0_interrupt (void) interrupt 4 {unsigned char byte; If (ri0 = 1) {ri0 = 0; // clear interrupt flag byte = sbuf0; // read a character from uartswitch (byte) {Case 0xb0: led1 = 1; led2 = 0; break; Case 0xb1: led1 = 0; led2 = 1; break; default: break;} If (ti0 = 1) // check if transmit flag is set {ti0 = 0; // clear interrupt flag} void uart_send (unsigned char m) {While (ti0 = 1); sbuf0 = m;} void delay_ms (unsigned int m) {While (M --) delay (2046);} void delay (unsigned int m) {While (M --);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.