ZigBee教程.doc

上传人:11****ws 文档编号:3007400 上传时间:2019-05-16 格式:DOC 页数:135 大小:852.50KB
下载 相关 举报
ZigBee教程.doc_第1页
第1页 / 共135页
ZigBee教程.doc_第2页
第2页 / 共135页
ZigBee教程.doc_第3页
第3页 / 共135页
ZigBee教程.doc_第4页
第4页 / 共135页
ZigBee教程.doc_第5页
第5页 / 共135页
点击查看更多>>
资源描述

1、ZigBee 学习之 1点灯我用的编译环境是 IARfor51 7.30B,再介绍一下我的开发板的情况, ZigBee 模块用的是 CC2430.这块芯片是一款Soc 的芯片,集成了 8051 内核和 ZigBee 射频部分,只要很少的外部电路就可以搭建一个射频模块。因为其中的是8051 的内核,所有对于熟悉 8051 系列的同学们来说,基本的一些操作就很简单了,这里我们在温习一下,顺便对CC2430 芯片进行一下熟悉。嵌入式的一大特点是其底层的软件和硬件紧密相关,如果没有数字电路的知识,那么作起来是很困难的。我们先介绍一下基本的管脚分配情况:P0 的 2 和 3 接到了一个 232 电压转换

2、芯片上,可以用来和 PC进行通信,LCD_RST,LCD_CS,接的是 LCD 的复位和片选端,这里我用到的 LCD 是兼容 PCD8544 的 NOkia5110 的LCD,为 SPI 总线 LCD,84X48 点阵。 CC2430_MOSI,CC2430_CLK 都接到 LCD 上了。P1_0 接到一个调试 LED 上面,为高电平点亮。下面的第一个实验就很简单了,我们只要把 P1_0 配置为通用 IO,输出方式,然后从这里输出高电平,那么就可以点亮这个 LED 了。用到的寄存器为:P1 配置寄存器 P1SEL,P1 方向寄存器 P1DIR程序如下:#include#define DEBUG

3、_LED P1_0void delay(unsigned int n)/26 cycles delaywhile(-n)asm(“NOP“);void LEDInit()P1SEL /P1_0 defined General purpose I/OP1DIR |= 0X01; /P1_0 defined Outputmain()LEDInit();while(1)DEBUG_LED = 1; /Led lightdelay(50000);DEBUG_LED = 0;delay(50000);编译通过以后就用 TI 的官方工具 SmartRF Flash Programmer 将生成的 HEX

4、文件,烧录到 CC2430 吧,激动的时刻,呵呵如果点亮了这个小小的 LED 那么恭喜你,你的硬件因该是没有问的了,一个小小的胜利后我们就有更多的勇气和信心前进咯!ZigBee 学习之 2SPIvoid LCD_IOInit()P0SEL P0DIR |= 0X03;P1SEL P1DIR |= 0XF0;void LCD_WriteOneByte(uchar data)uchar i = 0;for(i=0;ivoid delay(uint n)/26 cycles delaywhile(-n)asm(“NOP“);void LEDInit()P1SEL /P1_0 defined Gene

5、ral purpose I/OP1DIR |= 0X01; /P1_0 defined OutputDEBUG_LED = 1; /LED light up/=/Initial P1-USART1-SPIvoid SPIInit_U1_P1(void)PERCFG |= 0X02; /USART1.alt.2,P1P1SEL |= 0XE0; /P1,ISP,P1_4(LCD_DnC) defined general IOP2SEL |= 0X40; /USART1 has priorityU1GCR |= 0x20; /MSB first,Negative clock polarity,Da

6、ta is output on MOSI on the falling edge of CLKU1GCR |= 19; /MAX Baud rate(17),falling edgeU1BAUD = 0;UTX1IF = 0; /Clear interrupt/=/P1-USART1-SPI Send One Bytevoid SPI_SendOne_U1_P1(uchar data)U1DBUF = data;while(!UTX1IF);UTX1IF = 0;/=/P1-USART1-SPI Send Multily Bytevoid SPI_Send_U1_P1(uchar *data,

7、uint length)while(length)SPI_SendOne_U1_P1(*data);data+;length -= 1;/=/LCD Use SPI BUS,Set the first display piex./When after call this function can direct call SPI data output function/0X83,0Y5void SPI_LCDSetPos(uchar X,uchar Y)LCD_DnC = 0;SPI_SendOne_U1_P1(0x80 | X);SPI_SendOne_U1_P1(0x40 | Y);LCD

8、_DnC = 1;/=/LCD Use SPI BUS,Clear the LCD Display and its RAM.void SPI_ClearLCD()uint i;SPI_LCDSetPos(0,0);for (i=0; i504; i+)SPI_SendOne_U1_P1(0);/=/LCD Use SPI BUS,Send one LCD Commandvoid SPI_LCDWriteOneComm(uchar command)LCD_DnC = 0;SPI_SendOne_U1_P1(command);/=/LCD Use SPI BUS,Send More than on

9、e LCD Commandvoid SPI_LCDWriteMulComm(uchar *command,uchar num)LCD_DnC = 0;SPI_Send_U1_P1(command,num);/=/LCD Use SPI BUS,Send More than one datavoid SPI_LCDWriteData(uchar *data,uchar length)LCD_DnC = 1;SPI_Send_U1_P1(data,length);/=/LCD Use SPI BUS,Display a english character Without set position,

10、The inverse argument control/the inverse video mode.When use font library call this function to display./InPut :character:The character want to display/ inverse :Inverse video mode(1) or not(0)/OutPut :/Created :longfan,2010.1.10/Modify :/=void SPI_LCDWriteChar(uchar character,uchar inverse)uchar li

11、ne;LCD_DnC = 1;if(inverse)for(line=0;line6;line+)SPI_SendOne_U1_P1(font6x8character-32line);elseSPI_Send_U1_P1(uchar *)font6x8character-32,6);/=/LCD Use SPI BUS,Display a english character at (X,Y),The inverse argument control/the inverse video mode.When use font library call this function to displa

12、y./InPut :character:The character want to display/ X,Y :The position want to display/ inverse :Inverse video mode(1) or not(0)/OutPut :/Created :longfan,2010.1.10/Modify :/=void SPI_LCDDISPChar(uchar character,uchar X,uchar Y,uchar inverse)uchar line;SPI_LCDSetPos(X,Y);if(inverse)for(line=0;line6;li

13、ne+)SPI_SendOne_U1_P1(font6x8character-32line);elseSPI_Send_U1_P1(uchar *)font6x8character-32,6);/=/LCD Use SPI BUS,Display a english string at (X,Y)void SPI_LCDWriteEnString(uchar *EnString,uchar X,uchar Y,uchar inverse)uchar line;SPI_LCDSetPos(X,Y);while(*EnString)uchar chara = *EnString;chara -=

14、32;if(inverse)for(line=0;line6;line+)SPI_SendOne_U1_P1(font6x8charaline);elseSPI_Send_U1_P1(uchar *)font6x8chara,6);EnString+;/=/LCD Use SPI BUS,Draw a AREA use Horizontal(thus the High pixel must be a multiple of 8)./When use dot matrix to display use this function./InPut :data :The dot matrix of t

15、he data want to display/ X,Y :The position want to display/ wide,high:The pixel of wide and high/OutPut :/Created :longfan,2010.1.10/Modify :/=void SPI_LCDDrawArea_H(uchar *data,uchar X,uchar Y,uchar wide,uchar high)uchar row;uchar i;SPI_LCDSetPos(X,Y); /Set positionrow = high/8; /calculate the row

16、numberfor(i=0;irow;i+)SPI_Send_U1_P1(data+(i*wide),wide);Y += 1;SPI_LCDSetPos(X,Y); /Set position/=/LCD Use SPI BUS,Initial LCDvoid SPI_LCDInit()P0DIR |= 0X03; /P00(LCD_nCS),P01(LCD_nRES) Defined outputP1DIR |= 0X10; /P1_4(LCD_DnC) defined OutpuP2DIR |= 1; /LCD_BKLLCD_nCS = 0; /LCD Enabledelay(100);

17、LCD_nRES = 0; /LCD RESERTdelay(100);LCD_nRES = 1;delay(100);LCD_BKL = 0; /D5 light up/Send Initial command/LCD_DnC = 0;/SPI_SendOne_U1_P1(0x21); /Chip is active,Horizontal addressing,Use extended instruction set/SPI_SendOne_U1_P1(0xC8); /Set Vop/SPI_SendOne_U1_P1(0x06); /Temperature coefficient/SPI_

18、SendOne_U1_P1(0x13); /1:48/SPI_SendOne_U1_P1(0x20); /Use basic instruction set/SPI_SendOne_U1_P1(0x0C); /Display mode is Normal modeuchar Init_Comm=0x21,0xC8,0x06,0x13,0x20,0X0C;SPI_LCDWriteMulComm(Init_Comm,6);SPI_ClearLCD(); /Clear Display RAMSPI_LCDWriteOneComm(0X0C);LCD_DnC = 1; /Send Display Da

19、tamain()uchar Hello= 0x00,0x7E,0x10,0x10,0x7E,0x00,/*“H“,0*/* (6 X 8 , 楷体_GB2312 )*/0x00,0x7E,0x52,0x52,0x42,0x00,/*“E“,1*/* (6 X 8 , 楷体_GB2312 )*/0x42,0x7E,0x42,0x40,0x40,0x00,/*“L“,2*/* (6 X 8 , 楷体_GB2312 )*/0x42,0x7E,0x42,0x40,0x40,0x00,/*“L“,3*/* (6 X 8 , 楷体_GB2312 )*/0x00,0x3C,0x42,0x42,0x3C,0x

20、00,/*“O“,4*/* (6 X 8 , 楷体_GB2312 )*/0x00,0x7E,0x10,0x10,0x7E,0x00,/*“H“,0*/* (6 X 8 , 楷体_GB2312 )*/0x00,0x7E,0x52,0x52,0x42,0x00,/*“E“,1*/* (6 X 8 , 楷体_GB2312 )*/0x42,0x7E,0x42,0x40,0x40,0x00,/*“L“,2*/* (6 X 8 , 楷体_GB2312 )*/0x42,0x7E,0x42,0x40,0x40,0x00,/*“L“,3*/* (6 X 8 , 楷体_GB2312 )*/0x00,0x3C,0x

21、42,0x42,0x3C,0x00,/*“O“,4*/* (6 X 8 , 楷体_GB2312 )*/;uchar *tring = “Hello,CC2430! i LOVE THIS!“;uchar *tring1= “Hello,CC2430!“;LEDInit();SPIInit_U1_P1();SPI_LCDInit();/SPI_Send_U1_P1(uchar *)EshineLOGO,sizeof(EshineLOGO)/sizeof(uchar);/delay(50000);/delay(50000);/delay(50000);/delay(50000);/delay(50

22、000);/SPI_ClearLCD();/PI_Send_U1_P1(Hello,sizeof(Hello)/sizeof(uchar);/SPI_LCDDISPChar(Q,0,0,1);/SPI_LCDWriteEnString(tring,10,0,0);/SPI_LCDWriteEnString(tring,0,2,1);/SPI_LCDDrawArea_H(Hello,40,4,30,16);/SPI_ClearLCD();/SPI_LCDSetPos(0,0);/SPI_Send_U1_P1(uchar *)LOGO,sizeof(LOGO)/sizeof(uchar);/SPI

23、_LCDWriteData(uchar *)LOGO,sizeof(LOGO)/8);SPI_LCDWriteData(uchar *)LOGO,sizeof(LOGO)/sizeof(uchar);ZigBee 学习之 3USART 之 UARTUART 在现在的 PC 是不常见了,但是在嵌入式领域里面依然是不可或缺的一个接口,不管是调试,红外还是其他基本的通信,用 UART 相对于其他总线来说总是来的简单方便一下,而且由于技术成熟开发起来也更加快捷。CC2430 自带了 USART,可以配置成 UART 或者 SPI 模式,由于 CC2430 管脚的限制,所以只能同时使用两个外设,我就搞不懂为啥 chipcon 将 CC2430 的管脚的 USART 还非设个位置 1 位置 2 干什么,只要固定一个位置,可以配置不是一样的灵活好用么?加了位置可变反而要写更多的代码进行更多的配置不是变的麻烦了么?关于 CC2430 的 UART 使用其实前面一节 SPIU0DBUF = *data+;while(!UTX0IF);UTX0IF = 0;我这里是通过查询传输完成中断标志来进行的,如果打开中断的话可以在中断服务程序中进行数据的处理,也就没必要通过查询的方式来判断 UART 是否发送完数据了!

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。