cortex-a8硬件基础练习

时间:2022-09-10 18:50:18

实验要求:定时通过串口打印adc,时间和温度,开关量检测和通过串口接收命令控制led小灯的动作

下面是整理的代码:

#include "s5pc100.h"
#include "uart.h"

extern void printf(const char *fmt, ...);
#define LM75_ADDR 0x48

//#define VIC0ADDRESS *(volatile unsigned int*)0xe4000f00
#define VIC1VECTADDR10 *(volatile unsigned int*)0xe4100128
#define VIC0VECTADDR25 *(volatile unsigned int*)0xe4000164
#define VIC0VECTADDR22 *(volatile unsigned int*)0xE4000158
#define VIC1INTENABLE *(volatile unsigned int*)0xe4100010
#define VIC0INTENABLE *(volatile unsigned int*)0xe4000010

// BEEP
// GPD1 -> BEEP 1-on, 0-off
#define GPDCON *(volatile unsigned int*)0xe0300080
#define GPDDAT *(volatile unsigned int*)0xe0300084

//////////////////////////////////////////////////////////////
//////// key
#define GPH0CON *(volatile unsigned int*)0xe0300c00
#define GPH0DAT *(volatile unsigned int*)0xe0300c04

/*
GPG3_0 -> led4 GPG3_0 = 1 --> led On
GPG3_1 -> led1
GPG3_2 -> led2
GPG3_3 -> led3
*/

/////////////////////////////////////////////////////////////////////
// LED Driver
#define GPG3CON *(volatile unsigned int*)0xe03001c0
#define GPG3DAT *(volatile unsigned int*)0xe03001c4
#define LED_0 0x01
#define LED_1 0x02
#define LED_2 0x04
#define LED_3 0x08
#define LED_ALL 0x0f
#define LED_ON 0
#define LED_OFF 1

// ADC driver
#define ADCCON *(volatile unsigned int*)0xf3000000
#define ADCDLY *(volatile unsigned int*)0xf3000008
#define ADCDAT0 *(volatile unsigned int*)0xf300000c
#define ADCDAT1 *(volatile unsigned int*)0xf3000010
#define ADCMUX *(volatile unsigned int*)0xf300001c

/****************************************************************************
UART 0 Driver //串口驱动

UART_RXD0 -> GPA0_0 //串口发送和接收位对应芯片的引脚
UART_TXD0 -> GPA0_1
*/


#define GPA0CON *(volatile unsigned int *)0xe0300000
#define ULCON0 *(volatile unsigned int *)0xec000000
#define UCON0 *(volatile unsigned int *)0xec000004
#define UTRSTAT0 *(volatile unsigned int *)0xec000010
#define UTXH0 *(volatile unsigned int *)0xec000020
#define URXH0 *(volatile unsigned int *)0xec000024
#define UBRDIV0 *(volatile unsigned int *)0xec000028
#define UDIVSLOT0 *(volatile unsigned int *)0xec00002c
#define UINTP0 *(volatile unsigned int *)0xEC000030
#define UINTSP0 *(volatile unsigned int *)0xEC000034
#define UINTM0 *(volatile unsigned int *)0xEC000038

#define UART_RXD_MASK 0x01
#define UART_ERR_MASK 0x02
#define UART_TXD_MASK 0x04
#define UART_MODEM_MASK 0x08

//////////////////////////////////////////////////////////////////////////////////////
// Timer4 Control
#define TCFG0 *(volatile unsigned int *)0xea000000
#define TCFG1 *(volatile unsigned int *)0xea000004
#define TCON *(volatile unsigned int *)0xea000008
#define TCNTB1 *(volatile unsigned int *)0xea000018
#define TCMPB1 *(volatile unsigned int *)0xea00001c
#define TCNTB4 *(volatile unsigned int *)0xea00003c

////////////////////////////////////////////////////////////////////////////
// i2c driver
// GPD3 -> SDA0
// GPD4 -> SCL
#define I2CCON0 *(volatile unsigned int*)0xEC100000
#define I2CSTAT0 *(volatile unsigned int*)0xEC100004
#define I2CDS0 *(volatile unsigned int*)0xEC10000C
#define I2CLC0 *(volatile unsigned int*)0xEC100010

/////////////////////////////////////////////////////////////////////
//////////RTC Driver

#define RTCOCN *(volatile unsigned int*)0XEA300040
#define BCDSEC *(volatile unsigned int*)0XEA300070
#define BCDMIN *(volatile unsigned int*)0XEA300074
#define BCDHOUR *(volatile unsigned int*)0XEA300078
#define BCDDATE *(volatile unsigned int*)0XEA30007C
#define BCDDAY *(volatile unsigned int*)0XEA300080
#define BCDMON *(volatile unsigned int*)0XEA300084
#define BCDYEAR *(volatile unsigned int*)0XEA300088

char *signal_on = "led 1 on" ;
char *signal_off = "led 1 off" ;
//char signal_on[][10]={"led 1 on","led 2 on","led 3 on","led 4 on"};
//char signal_off[][10]={"led 1 off","led 2 off","led 3 off","led 4 off"};
char buf[20];
int cnt = 0;
int k = 0; //输入字符缓冲区长度
char dat;
int key;
int adc_val;
int flag_tem = 0;
int flag_led = 0;
int lm75_val = 0;


int strcmp(char *dest1,char *src1)
{
char *dest;
char *src ;
dest = dest1;
src = src1;
while(*dest != '\0')
{
if(*dest++ == *src++){}
else
{
return 0;
}
}
if(*src == '\0')
{
return 1;
}
else
{
return 0;
}
}

void rtc_init(void)
{
RTCCON = 0X01; //打开写
BCDSEC = 0X00;
BCDMIN = 0X28;
BCDHOUR = 0X14;
BCDDATE = 0X25;
BCDMON = 0X03;
BCDYEAR = 0X15;
RTCCON = 0X00; //关闭写
}

void beep_init(void)
{
GPDCON = GPDCON & 0xffffff0f;
GPDCON = GPDCON | 0x20;
}

void key_init(void)
{
GPH0CON &= ~((0x0f << 4) | (0x0f << 8) | (0x0f << 12) | (0x0f << 16) | (0x0f << 24)
| (0x0f << 28));
}

 

void adc_init(void)
{
ADCCON = (0x01 << 14) // prescaler enable
| (65 << 6) // prescaler value, clk = 66MHz / 66 = 1MHz
| (0x01 << 1); // read start

}

int adc_get(void)
{
int val;

val = ADCDAT0;
while ((ADCCON & (0x01 << 15)) == 0x00) {
}
val = ADCDAT0 & 0x3ff;
return val;
}

 


void led_on(int index)
{
GPG3DAT |= index;
}
void led_off(int index)
{
GPG3DAT &= ~index;
}
void led_init(void)
{
GPG3CON = GPG3CON & 0xffff0000;
GPG3CON = GPG3CON | 0x00001111; // set gpio as output
led_off(LED_ALL);
}


void delay(int dly)
{
volatile unsigned int i;
while (dly --)
for (i = 0; i < 1000; i ++);
}

 

void uart0_enable_interrupt(int mask)
{
UINTM0 &= ~mask;
}

void uart0_disable_interrupt(int mask)
{
UINTM0 |= mask;
}

char uart0_receive(void)
{
char dat;

while((UTRSTAT0 & 0X01) != 0X01){}
dat = URXH0;
return dat;
}

void uart0_send_char(char dat)
{
while((UTRSTAT0 & 0X04) != 0X04){}
UTXH0 = dat;
}


void uart0_isr(void)
{
int src;
src = UINTSP0;
TINT_CSTAT &= ~(0x01 << 4);
if(src & UART_RXD_MASK)
{
dat = URXH0;
if(dat != '\r')
{
buf[k++] = dat;
}
else
{
TINT_CSTAT |= 0x01 << 4;
flag_led = 1;
uart0_send_char('\n');
}
uart0_send_char(dat);
}

UINTP0 = 0XFF;

}

void myuart0_init(void)
{
GPA0CON &= ~((0x0f << 0) | (0x0f << 4)); //初始化串口的接收和发送
GPA0CON |= 0X02 | (0X02 << 4);

UBRDIV0 = 34; //设置波特率的整数位和小数位
UDIVSLOT0 = 0XDDDD;

ULCON0 = 0X03; //设置串口线路控制寄存器 ,这里是八位一发送

UCON0 = (0X01 << 0) | (0X01 << 2); //串口控制器,这里设置中断方式

uart0_enable_interrupt(UART_RXD_MASK); //使能接收中断

uart0_disable_interrupt(UART_ERR_MASK | UART_MODEM_MASK | UART_TXD_MASK); //屏蔽其他中断

VIC1VECTADDR10 = (unsigned int)uart0_isr; //设置串口向量地址

VIC1INTENABLE |= 0X01 << 10; //使能串口向量
}


void do_irq(void)
{
((void(*)(void))VIC0ADDRESS)();
VIC0ADDRESS = 0;
VIC1ADDRESS = 0;
VIC2ADDRESS = 0;
}

//////////////////////////////////////////////////////////////////////////////////////
// Timer 4 Control

void timer4_isr(void)
{
cnt ++;
if(cnt == 1000)
{
led_on(LED_0);
}
if (cnt == 2000) {
cnt = 0;
led_off(LED_0);
flag_tem = 1;
}

TINT_CSTAT |= 0x01 << 9;
}

void timer4_init(void)
{
// 2. set prescaler
TCFG0 &= ~(0xff << 8);
TCFG0 |= (65 << 8); // PCLK = 66MHz, TCLK = 66 / (65 + 1) = 1MHz

// 3. set mux
TCFG1 &= ~(0x0f << 16); // set mux = 1/1

// 4. set TCNTB
TCNTB4 = 1000;

// 5. 鎶婂€兼洿鏂板埌TCNT
TCON &= ~(0x0f << 20);
TCON |= 0x01 << 21; // update to TCNT

// 6. start timer
TCON &= ~(0x0f << 20);
TCON |= (0x05 << 20);

/////////////////////////////////////////////////////////////////////////////
// Set interrupt
// 7. set VICxVECTADDRx
VIC0VECTADDR25 = (unsigned int)timer4_isr;

// 8. enable interrupt in VIC0INTENABLE
VIC0INTENABLE |= 0x01 << 25;

// 9. Enable interrupt in timer
TINT_CSTAT |= 0x01 << 4;
}

////////////////////////////////////////////////////////////////////////////////
//////////timer1 driver

void timer1_init(void)
{
// 2. set prescaler
TCFG0 &= ~0xff;
TCFG0 |= 65; // PCLK = 66MHz, TCLK = 66 / (65 + 1) = 1MHz

// 3. set mux
TCFG1 &= ~(0x0f << 4); // set mux = 1/1

// 4. set TCMPB1 and TCNTB1
TCMPB1 = 250;
TCNTB1 = 500;

// 5. 鎶婂€兼洿鏂板埌TCNT 鍜孴CMPB涓? TCON &= ~(0x0f << 8);
TCON |= 0x01 << 9; // update to TCNT and TCMP

// 6. start timer
TCON &= ~(0x0f << 8);
TCON |= (0x08 << 8);


}
void stop_timer1(void)
{
TCON &= ~(0x01 << 8);
}
void start_timer1(void)
{
TCON |= 0x01 << 8;
}

void i2c_init(void)
{
GPDCON &= ~((0x0f << 12) | (0x0f << 16));
GPDCON |= (0x02 << 12) | (0x02 << 16);
I2CCON0 = 0xe0;
}
int i2c_master_transfer(char saddr, char *buf, int len)
{
I2CDS0 = saddr;
I2CSTAT0 = 0xf0;
// wait ack
while ((I2CCON0 & (0x01 << 4)) == 0x00) {
}

while (len --) {
I2CDS0 = *buf++;
// clear pending
I2CCON0 &= ~(0x01 << 4);
// wait ack
while ((I2CCON0 & (0x01 << 4)) == 0x00) {
}
}
I2CSTAT0 = 0xd0;
// clear pending
I2CCON0 &= ~(0x01 << 4);
return len;
}
int i2c_master_receive(char saddr, char *buf, int len)
{
I2CDS0 = saddr;
I2CSTAT0 = 0xb0;
// wait ack
while ((I2CCON0 & (0x01 << 4)) == 0x00) {
}

while (len -- > 0) {
I2CCON0 &= ~(0x01 << 4); // resume
// wait ack
while ((I2CCON0 & (0x01 << 4)) == 0x00) {
}
*buf ++ = I2CDS0;
}
I2CSTAT0 = 0x90;
// clear pending
I2CCON0 &= ~(0x01 << 4);
return len;

}
////////////////////////////////////////////////////////////////////////////
// LM75 driver

int lm75_read(void)
{
char buf[2];
buf[0] = 0;
i2c_master_transfer(LM75_ADDR << 1, buf, 1);
printf("\n"); //延时的功能
i2c_master_receive((LM75_ADDR << 1) | 0x01, buf, 2);
return (buf[0] << 8) | buf[1];
}

void printf_tem(void)
{
printf("time is %x:%x:%x\n",BCDHOUR,BCDMIN,BCDSEC);
printf("adc = %d mv\n", 3300 * adc_val / 1024);
if (lm75_val & 0x80)
{
printf("Temperture = %d.5\n", lm75_val >> 8);
printf("\n");
}
else
{
printf("Temperture = %d.0\n", lm75_val >> 8);
printf("\n");
}

}

void temp_over(void)
{
if((lm75_val >> 8) > 26) //因为采集精度问题,温度到27度时会报警
{
led_on(LED_2);
start_timer1();
}
else
{
led_off(LED_2);
stop_timer1();
}
}

void show_key_led(void)
{
if((key & 0xde) != 0xde) //按键k1
{
led_on(LED_3);
}
else
{
led_off(LED_3);
}
}

void show_uart_led(void)
{

if(strcmp(signal_on,buf))
{
led_on(LED_1);
}
else if(strcmp(signal_off,buf))
{
led_off(LED_1);
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Application
int main()
{
cnt = 0;
flag_tem = 0;
key_init();
led_init();
//rtc_init(); //第一次写时间的时候打开,之后可不用再初始化,时钟自动走时
timer4_init();
myuart0_init();
timer1_init();
adc_init();
i2c_init();
beep_init();

while (1)
{
key = GPH0DAT;
show_key_led();
if(flag_tem == 1)
{
flag_tem = 0;
adc_val = adc_get();
lm75_val = lm75_read();
printf_tem();

}
temp_over();

if(flag_led)
{
flag_led = 0;
buf[k] = '\0';;
k=0;
show_uart_led();
}
}
}

 

总结一下:定时器4用作两秒钟打印一次模拟量的及时,在定时器中断里面设置标志位,在主函数里面打印。定时器1用作蜂鸣器的驱动,串口程序用来接收和发送命令到终端

通过I2C的读写时序实现温度传感器lm75温度的读取,这段程序的编写可参考数据手册的说明,adc模拟量的采集的对象是根据硬件来定的,学习的板子是采集电压,初始化函

数包括了时钟的设置,采集精度,开始位的设置,串口打印用到的printf函数最终是通过串口打印的,这个函数移植老师的原函数的,难点:串口中接收命令并提取出来。中断

处理的原则是越快越好,在中断里面避免做大量的操作,串口接收命令时这里用定义的全局变量的数组来存放,在判断命令接收结束的时候需要在数组结尾加上‘\0’,作为字符串

的结束标志,这个工作我是在flag_led标志被置为后在主函数里面处理的。得到命令字符串后然后自己编写一个字符窜比较函数来判断命令的有效性,根据命令的不同做不同的

硬件动作,这里可以进一步扩展,点亮led 1只是个参考。