1.LED
1.需求:点亮LED4
2.看原理图,确定硬件原理,确定管脚XvVSYNC_LDI 给高电平--输出1--灯亮GPIO:通用输入输出端口 GPF3_4 输出高电平--灯亮GP端口 F组号
3.芯片手册GPF3CON:0x114001E0 [19:16] 0x1 = OutputGPF3DAT:0x114001E4 运行步骤:1.loadb 41000000 //指定下载到的地址2.传输文件3.go 41000000 //开始从41000000这个地址运行
/*===============================================
* 文件名称:main.c
* 创 建 者: memories
* 创建日期:2023年06月26日
* 描 述:have a nice day
================================================*/
#define GPF3CON *(volatile unsigned int *)0x114001E0
#define GPF3DAT *(volatile unsigned int *)0x114001E4void mydelay(int i)
{int x;while(i--)for(x=0;x<1000;x++);
}int main(int argc, char *argv[])
{ //配置GPF3CONGPF3CON &= ~(0xf << 16);GPF3CON |= 1 << 16;//配置GPF3DATwhile(1){//led onGPF3DAT |= 1 << 4;mydelay(1000);//led offGPF3DAT &= ~(1 << 4);mydelay(1000);}
}
2.串口
串口概念:收发数据的接口,数据传输的一种协议
单工:一条数据线,数据只能从A端到B端
半双工:一条数据线,数据可以从A端到B端,也可以从B端到A端,不能同时发生
全双工:两条数据线,数据的收发互不影响 (串口使用全双工)
串口协议:波特率:每秒发送的比特位数(你发送的快,我接受的慢有可能会造成数据的损坏,所以要设置波特率) 一般115200 9600数据位:每个数据帧中有多少位是有效的数据 一般 8位 9位起始位:每个数据帧起始的地方停止位:每个数据帧结束的位置, 0.5位 1位 1.5位 2位校验位:奇偶校验硬件流控制:不要
2.1串口通信
1.需求:板子给超级终端发送一个小写字母,板子回复显示出大写字母
2.原理图:BUF_XuTXD2 -- 发送引脚 -- GPA1_1
3.芯片手册:GPIO:GPA1CON: 0x11400020 [7:4] 0x2 = UART_2_TXDUART:ULCONn: 0x13820000 [5:3] 0xx = No parity[2] 0 = One stop bit per frame (默认值为0)[1:0] 11 = 8 bitsUCONn: 0x13820004 [3:2] 01 = Interrupt request or polling modeUTRSTATn: 0x13820010 [2] 1 = Transmitter empty (发送完成)UTXHn: 0x13820020 [7:0] data = 'A'UBRDIVn: 0x13820028 [15:0] 整数部分UFRACVALn: 0x1382002C [3:0] 小数部分DIV_VAL = UBRDIVn + UFRACVALn/16
or
DIV_VAL = (SCLK_UART/(bps * 16)) - 1
bps:115200
SCLK_UART:100M
{DIV_VAL = (100000000 / (115200 * 16)) - 1= (100000000 / 1843200) - 1= 54.25 - 1= 53.2553.25 = UBRDIVn + UFRACVALn/16UBRDIVn = 53UFRACVALn = 0.25 * 16 = 4
}
/*===============================================
* 文件名称:main1.c
* 创 建 者: memories
* 创建日期:2023年06月26日
* 描 述:have a nice day
================================================*/
#define GPA1CON *(volatile unsigned int *)0x11400020
#define ULCON2 *(volatile unsigned int *)0x13820000
#define UCON2 *(volatile unsigned int *)0x13820004
#define UTRSTAT2 *(volatile unsigned int *)0x13820010
#define UTXH2 *(volatile unsigned int *)0x13820020
#define URXH2 *(volatile unsigned int *)0x13820024
#define UBRDIV2 *(volatile unsigned int *)0x13820028
#define UFRACVAL2 *(volatile unsigned int *)0x1382002Cvoid mydelay(int i)
{int x;while(i--)for(x=0;x<1000;x++);
}
char recv_data()
{while(!(UTRSTAT2 & 1));return URXH2;
}void send_data(char ch)
{while(!(UTRSTAT2 & (1<<2)));UTXH2 = ch;
}int main(int argc, char *argv[])
{ //设置波特率UBRDIV2 = 53;UFRACVAL2 = 4;//设置串口的工作模式GPA1CON &= ~(0xf << 4);GPA1CON |= (0x2 << 4);//设置UART属性ULCON2 = 0x3;UCON2 &= ~(0x3 << 2);UCON2 |= 1 << 2; //开启发送轮循模式UCON2 &= ~(0x3);UCON2 |= 1 ; //开启接收轮循模式while(1){char ch;ch=recv_data();send_data(ch-32);}
}
3.RTC
芯片内部的一个实时时钟
1.需求:开启实时时钟,并发送到串口
2.芯片内部的功能,原理图没有
3.芯片手册:GPIOUARTRTC:RTCCON: 0x10070040 [0] 1 = Enables RTC controlBCDSEC: 0x10070070 [6:4] 0 to 5[3:0] 0 to 9BCDMIN: 0x10070074 [6:4] 0 to 5[3:0] 0 to 9BCDHOUR: 0x10070078 [5:4] 0 to 2[3:0] 0 to 9BCDDAYWEEK: 0x1007007C [2:0] 1 to 7BCDDAY: 0x10070080 [5:4] 0 to 3[3:0] 0 to 9BCDMON: 0x10070084 [4] 0 to 1[3:0] 0 to 9BCDYEAR: 0x10070088 [11:8] 0 to 9[7:4] 0 to 9[3:0] 0 to 9
/*===============================================
* 文件名称:main3.h
* 创 建 者: memories
* 创建日期:2023年06月27日
* 描 述:have a nice day
================================================*/
#ifndef __main3_H_
#define __main3_H_
#define GPA1CON *(volatile unsigned int *)0x11400020
#define ULCON2 *(volatile unsigned int *)0x13820000
#define UCON2 *(volatile unsigned int *)0x13820004
#define UTRSTAT2 *(volatile unsigned int *)0x13820010
#define UTXH2 *(volatile unsigned int *)0x13820020
#define URXH2 *(volatile unsigned int *)0x13820024
#define UBRDIV2 *(volatile unsigned int *)0x13820028
#define UFRACVAL2 *(volatile unsigned int *)0x1382002C
#define RTCCON *(volatile unsigned int *)0x10070040
#define BCDSEC *(volatile unsigned int *)0x10070070
#define BCDMIN *(volatile unsigned int *)0x10070074
#define BCDHOUR *(volatile unsigned int *)0x10070078
#define BCDDAYWEEK *(volatile unsigned int *)0x10070080
#define BCDDAY *(volatile unsigned int *)0x1007007C
#define BCDMON *(volatile unsigned int *)0x10070084
#define BCDYEAR *(volatile unsigned int *)0x10070088void UART_init();
void RTC_init();
void print_data();
void show_node(unsigned int data,int flag);
void send_char(char ch);#endif/*===============================================
* 文件名称:main3.c
* 创 建 者: memories
* 创建日期:2023年06月27日
* 描 述:have a nice day
================================================*/
#include "main3.h"int main(int argc, char *argv[])
{ UART_init();RTC_init();int sec=0;while(1){while(sec != BCDSEC){sec = BCDSEC;print_data();}}} void UART_init()
{//设置波特率UBRDIV2 = 53;UFRACVAL2 = 4;//设置串口的工作模式GPA1CON &= ~(0xf << 4);GPA1CON |= (0x2 << 4);//设置UART属性ULCON2 = 0x3;UCON2 &= ~(0x3 << 2);UCON2 |= 1 << 2; //开启发送轮循模式}void RTC_init()
{RTCCON |= 0x1;BCDSEC = 0x46;BCDMIN = 0x58;BCDHOUR = 0x13;BCDDAYWEEK = 2;BCDDAY = 0x27;BCDMON = 0x6;BCDYEAR = 0x023;
}void print_data()
{send_char('2');show_node(BCDYEAR,1);send_char('-');show_node(BCDMON,0);send_char('-');show_node(BCDDAY,0);send_char(' ');show_node(BCDDAYWEEK,0);send_char(' ');show_node(BCDHOUR,0);send_char(':');show_node(BCDMIN,0);send_char(':');show_node(BCDSEC,0);send_char('\n');send_char('\r');
}void show_node(unsigned int data,int flag)
{if(flag == 1)send_char((data >> 8 & 0xf) + '0');send_char((data >> 4 & 0xf) + '0');send_char((data & 0xf) + '0');}void send_char(char ch)
{while(!(UTRSTAT2 & 1<<2));UTXH2 = ch;
}
4.ADC
1.需求:启动AD模块,将模拟电压转换成数字发送到串口
2.原理图:ADC模块 --> 滑动变阻器 --> XadcAIN3 --> 专用引脚
3.芯片手册:ADC:ADC_CFG: 0x10010118 [16] 0 : General ADCADCCON: 0x126C0000 [16] 1 = 12bit A/D conversion[15] 1 = End of A/D conversion (Read Only)[2] 0 = Normal operation mode ?[1] 1 = Enables start by read operationADCDAT: 0x126C000C [11:0] read dataADCMUX: 0x126C001C [3:0] 0011 = AIN 3
/*===============================================
* 文件名称:main4.h
* 创 建 者: memories
* 创建日期:2023年06月27日
* 描 述:have a nice day
================================================*/
#ifndef __main4_H_
#define __main4_H_#define ADC_CFG *(volatile unsigned int *)0x10010118
#define ADCCON *(volatile unsigned int *)0x126C0000
#define ADCDAT *(volatile unsigned int *)0x126C000C
#define ADCMUX *(volatile unsigned int *)0x126C001C
#define GPA1CON *(volatile unsigned int *)0x11400020
#define ULCON2 *(volatile unsigned int *)0x13820000
#define UCON2 *(volatile unsigned int *)0x13820004
#define UTRSTAT2 *(volatile unsigned int *)0x13820010
#define UTXH2 *(volatile unsigned int *)0x13820020
#define URXH2 *(volatile unsigned int *)0x13820024
#define UBRDIV2 *(volatile unsigned int *)0x13820028
#define UFRACVAL2 *(volatile unsigned int *)0x1382002Cvoid mydelay(int i);
void UART_init();
void ADC_init();
int read_data();
void print_data(int data);
void send_char(char ch);#endif/*===============================================
* 文件名称:main4.c
* 创 建 者: memories
* 创建日期:2023年06月27日
* 描 述:have a nice day
================================================*/
#include "main4.h"
int main(int argc, char *argv[])
{ int data;UART_init();ADC_init();while(1){data = read_data();print_data(data);mydelay(1000);}} void mydelay(int i)
{int x;while(i--)for(x=0;x<1000;x++);
}
void UART_init()
{//设置波特率UBRDIV2 = 53;UFRACVAL2 = 4;//设置串口的工作模式GPA1CON &= ~(0xf << 4);GPA1CON |= (0x2 << 4);//设置UART属性ULCON2 = 0x3;UCON2 &= ~(0x3 << 2);UCON2 |= 1 << 2; //开启发送轮循模式}void ADC_init()
{ADC_CFG &= ~(1 << 16);ADCMUX &= ~(0xf);ADCMUX |= 0x3;ADCCON |= (1 << 16);ADCCON |= (1 << 14);ADCCON &= ~(1<<2);ADCCON |= 1<<1;}
int read_data()
{return ADCDAT & 0xfff;
}void print_data(int data)
{int val = data * 180/4096;send_char(val/100 + '0');send_char('.');send_char((val/10)%10 + '0');send_char(val%10 + '0');send_char('V');send_char('\n');send_char('\r');
}void send_char(char ch)
{while(!(UTRSTAT2 & 1<<2));UTXH2 = ch;
}