/****************************************************************************************************************
*************************
*Author:JYW
*Time:2019_03_30
*Describe:LetAllLinesOfCodeSpeak
昨夜西风凋碧树,独上高楼,望尽天涯路
*****************************************************/
/*******************************************
*Auther:JYW
*Time:2019_03_30
*Describe:03基础实验23模拟PWM调光实验代码学习
理解PWM原理
********/
#include<iocc2530.h>
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
#define LED1 P1_0
#define CYCLE 6000
/*该函数以ms为单位*/
void DelayMS(uint msec)
{
uint i,j;
for(i= 0; i<msec; i++)
for(j= 0; j<535;j++);
}
void Delay(unsigned int t)
{
while(--t);
}
void InitLed()
{
P1DIR |= 0x01;//将P1_0设置为输出模式默认的是上拉,普通模式
LED1 = 1;
}
//cycle 周期,event 事件
/*bright是亮的时间,cycle是整个周期*/
void LedEvent(uint bright, int cycle)
{
LED1 = 0;
Delay(bright);
LED1 = 1;
Delay(cycle - bright);
}
void main()
{
unsigned int j,bright;
InitLed();
while(1)
{
for(j= 0;j<CYCLE;j++)
LedEvent(1,300);
LED1 = 1;
DelayMS(2000);
for(j=0;j<CYCLE;j++)
LedEvent(50,300);
LED1 = 1;
DelayMS(2000);
for(j=0;j<CYCLE;j++)
LedEvent(150,300);
LED1 = 1;
DelayMS(2000);
//逐渐变亮
for(bright=1;bright<1000;bright++)
LedEvent(bright,1000);
LED1 = 0;
DelayMS(2000);
for(bright=1000-1;bright>0;bright--)
LedEvent(bright,1000);
}
}
/*******************************************/
#include<ioCC2530.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define CYCLE 6000
#define LED1 P1_0
void DelayMS(uint ms)
{
uint a,b;
for(a=ms;a>0;a--)
for(b=535;b>0;b--);
}
void InitLed()
{
P1DIR |= 0x01;
LED1 = 1;
}
void LedEvent(uint bright, uint cycle)
{
LED1 = 0;
DelayMS(bright);
LED1 = 1;
DelayMS(cycle-bright);
}
void main()
{
uint bright,j;
InitLed();
while(1)
{
LED1 = 0;
DelayMS(2000);
LED1 = 1;
for(j = 0; j<CYCLE; j++)
LedEvent(1,300);
LED1 = 0;
DelayMS(2000);
LED1 = 1;
for(j=0;j<CYCLE; j++)
LedEvent(50,300);
LED1 = 0;
DelayMS(2000);
LED1 = 1;
for(j=0;j<CYCLE; j++)
LedEvent(150,300);
LED1 = 0;
DelayMS(2000);
//渐亮
LED1 = 1;
for(bright = 0; bright<1000;bright++)
LedEvent(bright,1000);
DelayMS(2000);
//渐灭
LED = 0;
for(bright = 1000; bright>0;bright--)
LedEvent(bright,1000);
DelayMS(2000);
}
}
/*******************************************
*Auther:JYW
*Time:2019_03_30
*Describe:03基础实验24模拟PWM调光实验代码学习
理解PWM原理
******************************/
#include<ioCC2530.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define LED1 P1_0
void InitLed()
{
P1DIR |= 0x01;
LED1 =1 ;
}
void InitT1()
{
CLKCONCMD &= ~0x40;//设置为外部32M
while(CLKCONSTA & 0x40);//为1时说明16M
CLKCONCMD &= ~0x07;//32M
CLKCONCMD |= 0x38;//0011 1000定时器输出250K
PERCFG |= 0x40;
P2SEL &= ~0x10;
P2DIR |= 0xC0;
P1DIR |= 0xFF;
P1SEL |= 0x01;
T1CC2H = 0x00;
T1CC2L = 0x32;
T1CC0H = 0x00;
T1CC0L = 0xFF;
T1CCTL2 = 0x1C;
T1CTL = 0x02;//模模式
}
void main()
{
InitLed();
InitT1();
while(1)
{}
}
/******************************************************
*Auther:JYW
*Time:2019_03_30
*Describe:03基础实验09串口通讯发送字符串实验代码学习
******************************/
#include<ioCC2530.h>
#include<string.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define TX_SIZE 20
#define TX_STRING "Hello Zigbee"
char TxData[TX_SIZE];
void DelayMS(uint msec)
{
uint i,j;
for(i=0;i<msec; i++)
for(j=0;j<535;j++);
}
void InitUart()
{
PERCFG = 0x00;
P0SEL = 0x0C;
P2DIR &= ~0xC0;
U0CSR |= 0x80;
}