Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1964881
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2011-11-20 16:24:49

//注:将P0.23作为模拟量输入管脚,将测量的电压值通过uart1发送给上位机。(波特率9600)
#include "LPC17xx.h"                                                    /* LPC17xx外设寄存器            */
#include
#include
/*********************************************************************************************************
  宏定义
*********************************************************************************************************/
#define BEEP           (1ul << 11)                                      /* 蜂鸣器P0.11                  */
#define BEEPINIT()     LPC_GPIO0->FIODIR |= BEEP                        /* 蜂鸣器关                     */
#define BEEPOFF()      LPC_GPIO0->FIOSET = BEEP                         /* 蜂鸣器关                     */
#define BEEPON()       LPC_GPIO0->FIOCLR = BEEP                         /* 蜂鸣器开                     */
#define UART_BPS        9600                                            /* 串口通信波特率               */
char    GcRcvBuf[20];                                                   /* AD采集到的数据               */
/*********************************************************************************************************
** Function name:       myDelay
** Descriptions:        软件延时
** input parameters:    ulTime
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void myDelay (uint32_t ulTime)
{
    uint32_t i;
   
    i = 0;
    while (ulTime--) {
        for (i = 0; i < 5000; i++);
    }
}
/*********************************************************************************************************
** Function name:       uart0Init
** Descriptions:        按默认值初始化串口0的引脚和通讯参数。设置为8位数据位,1位停止位,无奇偶校验
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void uart0Init (void)
{
    uint16_t usFdiv;
    LPC_PINCON->PINSEL0 |= (0x01 << 4)|(0x01 << 6);   
    LPC_UART0->LCR  = 0x83;                                             /* 允许设置波特率               */
    usFdiv = (SystemFrequency/4/16)/UART_BPS;                           /* 设置波特率                   */
    LPC_UART0->DLM  = usFdiv / 256;
    LPC_UART0->DLL  = usFdiv % 256;
    LPC_UART0->LCR  = 0x03;                                             /* 锁定波特率                   */
    LPC_UART0->FCR  = 0x06;
}
void uart1Init (void)
{
    uint16_t usFdiv;
    LPC_PINCON->PINSEL0 |= (1 << 30);
    LPC_PINCON->PINSEL1 |= (1 << 0);
    LPC_SC->PCONP  |= (1<<4);                                           /* 开启串口1功能模块            */
    LPC_UART1->LCR  = 0x83;                                             /* 允许设置波特率               */
    usFdiv = (SystemFrequency / 4 / 16) / 9600;                     /* 设置波特率                   */
    LPC_UART1->DLM  = usFdiv / 256;
    LPC_UART1->DLL  = usFdiv % 256;
    LPC_UART1->LCR  = 0x03;                                             /* 锁定波特率                   */
    LPC_UART1->FCR  = 0x87;                                             /* 使能FIFO,设置8个字节触发点  */
    NVIC_EnableIRQ(UART1_IRQn);
    NVIC_SetPriority(UART1_IRQn, 5);
    LPC_UART1->IER  = 0x01;                                             /* 使能接收中断                 */
}
/*********************************************************************************************************
** Function name:       uart0SendByte
** Descriptions:        从串口0发送数据
** input parameters:    data: 发送的数据
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void uart0SendByte (uint8_t ucData)
{
    LPC_UART0->THR   = ucData;
    while ( (LPC_UART0->LSR & 0x40) == 0 );
}
void uart1SendByte (uint8_t ucDat)
{
    LPC_UART1->THR = ucDat;                                             /* 写入数据                     */
    while ((LPC_UART1->LSR & 0x20) == 0);                               /* 等待数据发送完毕             */
}
/*********************************************************************************************************
** Function name:       pcDispChar
** Descriptions:        向PC机发送显示字符
** input parameters:    x:        显示字符的横坐标
**                      y:        显示字符的纵坐标
**                      ucChr:    显示的字符,不能为ff
**                      ucColor:  显示的状态,包括前景色、背景色、闪烁位。
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void pcDispChar (uint8_t x, uint8_t y, uint8_t ucChr, uint8_t ucColor)
{
 #if 0
    uart0SendByte(0xff);                                               /* 起始字符                     */
    uart0SendByte(x);
    uart0SendByte(y);
    uart0SendByte(ucChr);
    uart0SendByte(ucColor);
 #else
 uart1SendByte(0xff);                                               /* 起始字符                     */
    uart1SendByte(x);
    uart1SendByte(y);
    uart1SendByte(ucChr);
    uart1SendByte(ucColor);
 #endif
}
/*********************************************************************************************************
** Function name:       iSendStr
** Descriptions:        向上位机发送字符串
** input parameters:    x:        显示字符的横坐标
**                      y:        显示字符的纵坐标
**                      ucColor:  显示的状态,包括前景色、背景色、闪烁位。
**                                与DOS字符显示一样:0~3,前景色,4~6,背景色,7,闪烁位
**                      pcStr:   要发送的字符串,以'\0'结束
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void iSendStr (uint8_t x, uint8_t y, uint8_t ucColor, char *pcStr)
{
    while (1) {
        if (*pcStr == '\0') {                                           /* 结束字符                     */
            break;
        }
        pcDispChar(x++, y, *pcStr++, ucColor);
        if (x >= 80) {
            x = 0;
            y++;
        }
    }
}
/*********************************************************************************************************
** Function name:       uart0SendStr
** Descriptions:     向串口发送字符串
** input parameters:    pucStr:   要发送的字符串指针
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void uart0SendStr (uint8_t  const *pucStr)
{
    while (1) {
        if (*pucStr == '\0')break;                                      /* 遇到结束符,退出             */
        uart0SendByte (*pucStr++);
    }
}
/*********************************************************************************************************
* Function name:        adcInit
* Descriptions:         ADC初始化
* input parameters:     无
* output parameters:    无
* Returned value:       无
*********************************************************************************************************/
void adcInit (void)
{
    uint32_t ulTemp;
   
    LPC_SC->PCONP |= 1 << 12;                                           /* 打开ADC电源                  */
    LPC_PINCON->PINSEL1 |= (0x01 << 14);
    ulTemp = (24000000 / (13000000));
    ulTemp = (1 << 0)                                                   /* 选择AD0.0为AD输入引脚        */
           |(( ulTemp) << 8)                                            /* 转换时钟为13MHz              */
           |(0 << 16)                                                   /* BURST = 0,软件控制转换操作   */
           |(1 << 21)                                                   /* PDN = 1,正常工作模式        */
           |(1 << 24)                                                   /* 设置直接启动模式             */
           |(0 << 27 );                                                 /* 设置模式,直接启动模式下无效 */
    LPC_ADC->ADCR = ulTemp;
}
/*********************************************************************************************************
** Function name:      main
** Descriptions:       AD采集数据例程,需将跳线一端(AN0)连到JP22的P0.23脚,另一端连接待测电压端。同时将PC机的
**                     串口线连接到开发板UART0,然后短接JP14跳线组,打开Easyarm串口调试软件,观察采样结果
**                     注意:由于AD参考电压是3.0V,若直接采用P0.23采集电压,电压不得高于3.0V。若经过开发板
**                     的采集电路后,JP16端输入电压范围为-5V~+5V             
** input parameters:   无
** output parameters:  无
** Returned value:     无
*********************************************************************************************************/
int main (void)
{
    static uint32_t ulADCbuf;                                         /* AD采集数据缓冲区             */
    static uint32_t ulADCData;
    static uint8_t  i;
    SystemInit();                                                       /* 系统初始化,切勿删除         */
    //uart0Init();
 uart1Init();
    adcInit();
    while (1) {
   ulADCData = 0;
        for(i = 0;i < 8; i++) {
            LPC_ADC->ADCR |= 1 << 24;                                   /* 立即转换                     */
            while ((LPC_ADC->ADSTAT & (1 << 0)) == 0);                  /* 读取AD0STAT的通道0的Done     */
            LPC_ADC->ADCR |= (1 <<24);                                  /* 第一次转换结果丢弃           */
            while ((LPC_ADC->ADSTAT & (1 << 0)) == 0);                  /* 读取AD0STAT的通道0的Done     */
            ulADCbuf   = LPC_ADC->ADDR0;                                /* 只有一路,则读取全局寄存器   */
            ulADCbuf   = (ulADCbuf >> 4) & 0xfff;
            ulADCData += ulADCbuf;
        }
        ulADCData = (ulADCData/8);                                      /* 采样8次进行虑波处理          */
        ulADCData = (ulADCData*3000)/4096;
        sprintf(GcRcvBuf,"VIN0 = %4d mv",ulADCData);
        iSendStr(0,0,0x30,GcRcvBuf);                              /* 将数据发送到串口显示         */
        myDelay(20);
    }
}
/*********************************************************************************************************
  End Of File
*********************************************************************************************************/
说明:
当P0.23管脚悬空时,测出的电压是一个浮动的值。如下图:
当把该管脚连接到被测量端时,电压值为一个比较稳定的值,如下图:
阅读(2359) | 评论(0) | 转发(0) |
0

上一篇:LPC1766之串口

下一篇:IAP

给主人留下些什么吧!~~