Blog
私有云
聊天室
图床
登录
[TOC]
实验三
作者:
linzy
浏览:214
2021年11月26日 15:09
``` #include <stdarg.h> /************UART.c********************************************/ #define ULCON0 (*((volatile unsigned long *)0x7F005000)) #define UCON0 (*((volatile unsigned long *)0x7F005004)) #define UFCON0 (*((volatile unsigned long *)0x7F005008)) #define UMCON0 (*((volatile unsigned long *)0x7F00500C)) #define UTRSTAT0 (*((volatile unsigned long *)0x7F005010)) #define UFSTAT0 (*((volatile unsigned long *)0x7F005018)) #define UTXH0 (*((volatile unsigned char *)0x7F005020)) #define URXH0 (*((volatile unsigned char *)0x7F005024)) #define UBRDIV0 (*((volatile unsigned short *)0x7F005028)) #define UDIVSLOT0 (*((volatile unsigned short *)0x7F00502C)) #define GPAPUD (*((volatile unsigned long *)0x7F008008)) #define GPACON (*((volatile unsigned long *)0x7F008000)) #define rTCFG0 (*((volatile unsigned long *)0x7F006000)) #define rTCFG1 (*((volatile unsigned long *)0x7F006004)) #define rTCON (*((volatile unsigned long *)0x7F006008)) #define rTCNTB0 (*((volatile unsigned long *)0x7F00600C)) #define rTCMPB0 (*((volatile unsigned long *)0x7F006010)) #define rTCON (*((volatile unsigned long *)0x7F006008)) #define rADCCON (*((volatile unsigned long *)0x7E00B000)) #define rADCDAT0 (*((volatile unsigned long *)0x7E00B00C)) #define LOOP 10000 volatile int preScaler = 255; void delay(int times) { int i; for(;times>0;times--) for(i=0;i<3000;i++); } void init_uart(void) { GPACON = (GPACON & ~(0xff<<0)) | (0x22<<0); GPAPUD = (GPAPUD & ~(0xf<<0)) | (0x1<<0); // RXD0: Pull-down, TXD0: pull up/down disable ULCON0=ULCON0&(~0xF)|(0<<6)|(0<<3)|(0<<2)|(3<<0);; UCON0=UCON0&(~0x3F)|(0<<10)|(1<<9)|(1<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1<<0); UFCON0=UFCON0&(~0xF)|(0<<6)|(0<<4)|(0<<2)|(0<<1)|(0<<1); UMCON0 = (0<<5)|(0<<4)|(0<<0); UBRDIV0=34; UDIVSLOT0=0xDDDD; } char getchar(void) { while (!(UTRSTAT0&0x01)); /*RX_FIFO EMPTY,绛夊緟*/ return URXH0; } void putchar(char c) { while (!(UTRSTAT0&0x02)); /* TX_FIFO_FULL=1,绛夊緟*/ UTXH0 = c; } void Uart_Sendstring(char *str) { while(*str) putchar(*str++); } void Uart_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart_Sendstring(string); va_end(ap); } int ReadAdc(int ch) { int i; static int prevCh=-1; rADCCON=(1<<14)|(preScaler<<6)|(ch<<3); if(prevCh!=ch) { rADCCON=(1<<14)|(preScaler<<6)|(ch<<3); for(i =0;i<LOOP;i++); prevCh=ch; } rADCCON|=0x1; while(rADCCON&0x01); while(!(rADCCON&0x8000)); return ((int)rADCDAT0 & 0x3ff); } /***********************main.c************************************/ int res; int main() { char c; init_uart(); while (1) { delay(100); c = getchar(); res = ReadAdc(0); putchar(res); putchar(res>>8); } } ```
请
登录
后回复
共有0条评论
闽ICP备19009362号