1、URAT通信
实现思路,原理图,芯片手册,通过查看原理图,找到对应的引脚,然后进行查找芯片手册,找到对应的控制器,现在我们用到的就是UART2这个控制器,然后配置对应的寄存器,让其开启工作,这样一个编程我们称之为寄存器编程,
#include "exynos_4412.h"#define GPA1CON (*((unsigned int *)0x11400020))
#define ULCON (*((unsigned int *)0x13820000))
#define UCON (*((unsigned int *)0x13820004))
#define UBRDIV (*((unsigned int *)0x13820028))
#define UFRACVAL (*((unsigned int *)0x1382002c))
#define UTXH (*((unsigned int *)0x13820020))
#define URXH (*((unsigned int *)0x13820024))
#define UTRSTAT (*((unsigned int *)0x13820010))void uart_init()
{//初始化引脚 GPA1_0 GPA1_1GPA1CON = GPA1CON & ~0xff | 0x22;//初始化uart控制器//ULCON//ULCON = 0x3; //0b 0000011UART2.ULCON2 = 0x3;//UCON:开启了uart的发送与接收//UCON = UCON & ~0xf | 0x5; // 0101UART2.UCON2 = UART2.UCON2 & ~0xf | 0x5;//波特率//UBRDIV = 53;//UFRACVAL = 4;UART2.UBRDIV2 = 53;UART2.UFRACVAL2 = 4;
}void putc(char c)
{//判断之前的发送完没有(发送寄存器是否还有数据)while( !(UTRSTAT & 1 << 1) ) ;//赋值给发送寄存器,开始发送UTXH = c;
}void puts(char * str)
{while(*str){putc(*str++);}
}char getc(void)
{//串口控制器自动接收存储到接收寄存器//判断是否接收到数据while( !(UTRSTAT & 1) ) ;//从接收寄存器中读取字符char c;c = URXH;return c;
}int main()
{char c;uart_init();puts("hello world\r\n");while(1){c = getc();puts("recv:");putc(c);puts("\r\n");}//printf();打印//1、组织字符串:把要打印的内容转为字符串 "abcd %d %s",10,"hello" printf源码//2、发送字符串:串口操作 puts()return 0;
}
2、看门狗中断
#include"exynos_4412.h"
#include"uart.h"void delay()
{int i,j;for(i = 0;i < 1000;i++){for(j = 0;j < 10000;j++);}
}void wdt_init()
{//10s//复位模式//设置预分频WDT.WTCON = WDT.WTCON & ~(0xff<<8) | 249<<8;//设置固定分频WDT.WTCON = WDT.WTCON & ~(0x3<<3) | 3<<3;//设置复位WDT.WTCON |= 1;//配置重载器WDT.WTDAT = 31250;//配置计数器WDT.WTCNT = 31250;
}void wdt_start()
{//启动看门狗WDT.WTCON |= 1<<5;
}void wdt_stop()
{//关闭看门狗WDT.WTCON &= ~(1<<5);
}int main()
{int a = 100;char c;uart_init();//printf("hello!a=%d\r\n",a);puts("hello world\r\n");wdt_init();wdt_start();while(1){c = getc();//喂狗WDT.WTCNT = WDT.WTDAT;printf("recv data is :%c\r\n",c);//1、解析格式化字符串;2、puts()发送//delay();}return 0;
}
3、按键中断
#include"exynos_4412.h"
#include"uart.h"//裸机开发,设置key2中断void key2_exit_init()
{//GPIO控制器//中断初始化,引脚GPX1_1GPX1.CON=GPX1.CON & ~(0xf<<4)|(0xf<<4);//设置触发模式EXT_INT41_CON=EXT_INT41_CON & ~(0x7<<4) |(0x3<<4);//使能中断EXT_INT41_MASK=EXT_INT41_MASK & (~(0x1<<1));//GIC中断控制器//使能中断ICDDCR=1;//使能IDICDISER.ICDISER1=ICDISER.ICDISER1 | (1<<25);//设置优先级,先移位清0,再设置优先级ICDIPR.ICDIPR14=ICDIPR.ICDIPR14 & ~(0xff<<8)| (0x1<<8);//将信号发送给CPU0,通过一个40个寄存器管理,每个寄存器管理4个,每8位表示一个ID//ICDIPR14是对应管理的寄存器,设置这个寄存器会达到这样的操作ICDIPTR.ICDIPTR14=ICDIPTR.ICDIPTR14 & ~(0xff<<8) |(0x1<<8);//设置CPU使能,每个CPU对应一个寄存器CPU0.ICCICR=1;//设置过滤优先级CPU0.ICCPMR=CPU0.ICCPMR & ~(0xff) | 0xff;}void do_irq()
{int irq_num;//触发中断处理printf("irq\r\n");//获取到中断IDirq_num=CPU0.ICCIAR & 0x3ff;printf("irq_num=%d\r\n",irq_num);switch(irq_num){case 57://清除中断源的挂起EXT_INT41_PEND=EXT_INT41_PEND | 1<<1;//清除中断ID的挂起ICDICPR.ICDICPR1=ICDICPR.ICDICPR1 | 1<<25;break;case 58:break;};CPU0.ICCEOIR=irq_num;return ;}
int main()
{int a = 100;uart_init();printf("hello!a=%d\r\n",a);//开启按键中断key2_exit_init();while(1);return 0;
}