操作GPIO点亮LED
//head
#include "iostm8s208mb.h"
//normal datatype
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
//port define
#define LED0 PI_ODR_ODR0
// function declare
void led_init();
void delayms(u16 count);
//main
void main( void )
{
led_init();
while(1)
{
LED0=!LED0;
delayms(500);
};
}
// functions
void led_init()
{
PI_DDR_DDR0=1;
PI_CR1_C10=1;
PI_CR2_C20=0;
LED0=1;
}
void delayms(u16 count)
{
u8 i,j;
while(count--)
{
for(i=0;i<50;i++)
{
for(j=0;j<20;j++);
}
}
}
数码管驱动
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
//------------------------------function declare--------------------------------
void delay(u16 count);
//------------------------------main--------------------------------------------
u8 table[]={0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X77,0X7C,0X39,0X5E,0X79,0X71};
//------------------------------------------------------------------------------
void main( void )
{
PC_DDR=0XFF;
PC_CR1=0XFF;
PC_CR2=0X00;
PC_ODR=0X80;
while (1)
{
u8 i;
for(i=0;i<sizeof(table);i++)
{
PC_ODR=~table[i];
delay(300);
}
}
}
//------------------------------functions---------------------------------------
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
中断点亮LED
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LED PI_ODR_ODR0
//------------------------------function declare--------------------------------
void delay(u16 count);
void GPIO_init(void);
//------------------------------global------------------------------------------
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
asm("sim");
EXTI_CR1=0X20;
asm("rim");
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PI_DDR_DDR0=1;
PI_CR1_C10=1;
PI_CR2_C20=0;
PC_DDR_DDR0=0;
PC_CR1_C10=1;
PC_CR2_C20=1;
}
#pragma vector=0X07
__interrupt void EXTI_PORTC_IRQHandler(void)
{
delay(10);
if (PC_IDR_IDR0==0)
{
LED=!LED;
while(!PC_IDR_IDR0);
}
}
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
LCD1602
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDRS PF_ODR_ODR0
#define LCDRW PF_ODR_ODR3
#define LCDEN PF_ODR_ODR4
#define LCDDATA PB_ODR
//------------------------------function declare--------------------------------
void GPIO_init(void);
void delay(u8 count);
void LCD_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_display(void);
//------------------------------------------------------------------------------
u8 line1[]="==welcome!anny==";
u8 line2[]=">>>>>>>>>>>>>>>>";
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
LCD_init();
LCD_display();
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PF_DDR=0XFF;
PF_CR1=0XFF;
PF_CR2=0X00;
PF_ODR=0X00;
PB_DDR=0XFF;
PB_CR1=0XFF;
PB_CR2=0X00;
PB_ODR=0X00;
}
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<50;j++);
}
}
void LCD_init(void)
{
LCD_write(0X38,0);
LCD_write(0X0E,0);
LCD_write(0X06,0);
LCD_write(0X01,0);
}
void LCD_write(u8 data,u8 cmd_flag)
{
LCDRS=cmd_flag;
LCDRW=0;
LCDEN=1;
delay(1);
LCDDATA=data;
LCDEN=0;
}
void LCD_display(void)
{
LCD_write(0X80,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line1[i],1);
}
LCD_write(0XC0,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line2[i],1);
}
}
LCD12864(文字模式)
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDRS PF_ODR_ODR0
#define LCDRW PF_ODR_ODR3
#define LCDEN PF_ODR_ODR4
#define LCDPSB PF_ODR_ODR5
#define LCDDATA PB_ODR
//------------------------------function declare--------------------------------
void delay(u8 count);
void GPIO_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_init(void);
void LCD_display(u8 row,u8 col,u8 *string);
//------------------------------global------------------------------------------
//------------------------------main--------------------------------------------
void main( void )
{
delay(100);
GPIO_init();
LCD_init();
LCD_display(1,0,"基于ST7902控制器");
LCD_display(2,0,"================");
LCD_display(3,0,"使用LCD12864显示");
LCD_display(4,0,"屏并口显示数据!");
}
//------------------------------functions---------------------------------------
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
void GPIO_init(void)
{
//lcd DB0~DB7
PB_DDR |=0XFF;
PB_CR1 |=0XFF;
PB_CR2 &=0X00;
PB_ODR &=0X00;
//LCD RS RW EN PSB
PF_DDR |=0X39;
PF_CR1 |=0X39;
PF_CR2 &=0XC6;
PF_ODR &=0XC6;
LCDPSB=1;
LCDRW=0;
}
void LCD_write(u8 data,u8 cmd_flag)
{
LCDRS=cmd_flag;
LCDEN=1;
LCDDATA=data;
delay(1);
LCDEN=0;
}
void LCD_init(void)
{
LCD_write(0X0C,0);
delay(1);
LCD_write(0X30,0);
delay(1);
LCD_write(0X01,0);
delay(1);
}
void LCD_display(u8 row,u8 col,u8 *string)
{
switch (row)
{
case 1:row=0X80;break;
case 2:row=0X90;break;
case 3:row=0X88;break;
case 4:row=0X98;break;
default:row=0X80;
}
LCD_write(row+col,0);
while (*string != '\0')
{
LCD_write(*string,1);
string++;
}
}
LCD12864 图像模式
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDRS PF_ODR_ODR0
#define LCDRW PF_ODR_ODR3
#define LCDEN PF_ODR_ODR4
#define LCDPSB PF_ODR_ODR5
#define LCDDATA PB_ODR
//------------------------------function declare--------------------------------
void delay(u8 count);
void GPIO_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_init(void);
void LCD_pic(u8 *pic);
//------------- -----------------global------------------------------------------
u8 dis[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0xF8,0x7F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0xC3,0x0F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0xFF,0xC7,0x8F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFE,0x3F,0xF1,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFC,0x3F,0xF0,0xFF,0x80,0x03,0x00,0x00,0x00,0x30,0x60,0x00,0x00,0x00,0x00,
0x07,0xE3,0xFF,0xFF,0x1F,0x80,0x03,0x00,0x63,0xFC,0x30,0xE0,0x30,0xC4,0x08,0x60,
0x07,0xE3,0xFF,0xFF,0x8F,0x80,0xFF,0xFC,0x63,0x0C,0x30,0xC0,0xC6,0xFC,0x7C,0xF8,
0x07,0x8F,0xFC,0xFF,0xE7,0x80,0xC0,0x0C,0x63,0x0C,0xFD,0xFC,0xFF,0xC8,0xF8,0x5C,
0x07,0x8F,0xF0,0x3F,0xE7,0x80,0xC6,0x0D,0xFB,0x0C,0xCD,0x8C,0x02,0xCC,0x4F,0xE0,
0x07,0x8F,0xC0,0x0F,0xE7,0x80,0x06,0x00,0x5B,0xFC,0xCF,0x8C,0xFE,0xFC,0x0F,0xE0,
0x07,0x8F,0x80,0x07,0xE7,0x80,0x0E,0x00,0xDB,0x00,0xCD,0x0C,0xC6,0xC5,0xFF,0xFE,
0x07,0x8F,0xC0,0x0F,0xE7,0x81,0xFF,0xFE,0xDB,0x60,0xCC,0x8C,0xFE,0xDC,0xF0,0x1C,
0x07,0x8F,0xF0,0x3F,0xE7,0x80,0x18,0x70,0xDB,0x6C,0xFD,0xCC,0xC6,0xF8,0x7F,0xF8,
0x07,0x8F,0xFC,0xFF,0xE7,0x80,0x38,0x60,0xDB,0x7C,0xCC,0xCC,0xFE,0xC4,0x63,0x18,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x3E,0xE0,0xF3,0x78,0xCC,0xEC,0xC6,0xCC,0x63,0x18,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x0F,0xC0,0x3F,0x70,0xCC,0x6C,0xDE,0xFC,0x7F,0xF8,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x07,0xC0,0x7E,0x64,0xCC,0x4C,0x4C,0x98,0x60,0x0E,
0x07,0x8F,0x3F,0xF3,0xE7,0x80,0x1E,0xF0,0xF6,0x66,0xCC,0x0C,0xCC,0xCC,0x60,0x0C,
0x01,0x8F,0x0F,0xC3,0xE6,0x00,0x7C,0x39,0xCE,0x6C,0xFC,0x0C,0xCC,0xCC,0x70,0x1C,
0x00,0x8F,0x03,0x03,0xE4,0x00,0xE0,0x0C,0x84,0x7C,0xCC,0xF8,0x80,0x04,0x3F,0xF8,
0x06,0x0F,0x00,0x03,0xE1,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0x0F,0x00,0x03,0xC3,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xE3,0x00,0x03,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xF1,0x00,0x02,0x3F,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,
0x07,0xFE,0x00,0x01,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFF,0x00,0x03,0xFF,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,
0x01,0xFF,0xC0,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x7F,0xE0,0x1F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1F,0xF8,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//------------------------------main--------------------------------------------
void main( void )
{
CLK_CKDIVR=0X00;
delay(100);
GPIO_init();
LCD_init();
LCD_pic(dis);
}
//------------------------------functions---------------------------------------
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
void GPIO_init(void)
{
//lcd DB0~DB7
PB_DDR |=0XFF;
PB_CR1 |=0XFF;
PB_CR2 &=0X00;
PB_ODR &=0X00;
//LCD RS RW EN PSB
PF_DDR |=0X39;
PF_CR1 |=0X39;
PF_CR2 &=0XC6;
PF_ODR &=0XC6;
LCDPSB=1;
LCDRW=0;
}
void LCD_write(u8 data,u8 cmd_flag)
{
LCDRS=cmd_flag;
LCDEN=0;
delay(1);
LCDDATA=data;
LCDEN=1;
delay(1);
LCDEN=0;
delay(1);
}
void LCD_init(void)
{
LCD_write(0X0C,0);
delay(1);
LCD_write(0X34,0);
delay(1);
LCD_write(0X01,0);
delay(1);
}
void LCD_pic(u8 *pic)
{
u8 i,j,k;
LCD_write(0X34,0);delay(10);
for ( i = 0; i <2; i++)
{
for ( j = 0; j <32; j++)
{
LCD_write(0X80+j,0);
LCD_write(0X80+8*i,0);
for ( k = 0; k <16; k++)
{
LCD_write(*pic++,1);
}
}
}
LCD_write(0X36,0);delay(10);
LCD_write(0X30,0);delay(10);
}
LCD12864 串行传输
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDCS PF_ODR_ODR0
#define LCDDIO PF_ODR_ODR3
#define LCDCLK PF_ODR_ODR4
#define LCDPSB PF_ODR_ODR5
//------------------------------function declare--------------------------------
void delay(u8 count);
void GPIO_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_init(void);
void LCD_pic(u8 *pic);
void LCD_send_byte(u8 byte);
//------------- -----------------global------------------------------------------
u8 dis[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xFC,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x3F,0xF8,0x7F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0xC3,0x0F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0xFF,0xC7,0x8F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFE,0x3F,0xF1,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFC,0x3F,0xF0,0xFF,0x80,0x03,0x00,0x00,0x00,0x30,0x60,0x00,0x00,0x00,0x00,
0x07,0xE3,0xFF,0xFF,0x1F,0x80,0x03,0x00,0x63,0xFC,0x30,0xE0,0x30,0xC4,0x08,0x60,
0x07,0xE3,0xFF,0xFF,0x8F,0x80,0xFF,0xFC,0x63,0x0C,0x30,0xC0,0xC6,0xFC,0x7C,0xF8,
0x07,0x8F,0xFC,0xFF,0xE7,0x80,0xC0,0x0C,0x63,0x0C,0xFD,0xFC,0xFF,0xC8,0xF8,0x5C,
0x07,0x8F,0xF0,0x3F,0xE7,0x80,0xC6,0x0D,0xFB,0x0C,0xCD,0x8C,0x02,0xCC,0x4F,0xE0,
0x07,0x8F,0xC0,0x0F,0xE7,0x80,0x06,0x00,0x5B,0xFC,0xCF,0x8C,0xFE,0xFC,0x0F,0xE0,
0x07,0x8F,0x80,0x07,0xE7,0x80,0x0E,0x00,0xDB,0x00,0xCD,0x0C,0xC6,0xC5,0xFF,0xFE,
0x07,0x8F,0xC0,0x0F,0xE7,0x81,0xFF,0xFE,0xDB,0x60,0xCC,0x8C,0xFE,0xDC,0xF0,0x1C,
0x07,0x8F,0xF0,0x3F,0xE7,0x80,0x18,0x70,0xDB,0x6C,0xFD,0xCC,0xC6,0xF8,0x7F,0xF8,
0x07,0x8F,0xFC,0xFF,0xE7,0x80,0x38,0x60,0xDB,0x7C,0xCC,0xCC,0xFE,0xC4,0x63,0x18,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x3E,0xE0,0xF3,0x78,0xCC,0xEC,0xC6,0xCC,0x63,0x18,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x0F,0xC0,0x3F,0x70,0xCC,0x6C,0xDE,0xFC,0x7F,0xF8,
0x07,0x8F,0xFF,0xFF,0xE7,0x80,0x07,0xC0,0x7E,0x64,0xCC,0x4C,0x4C,0x98,0x60,0x0E,
0x07,0x8F,0x3F,0xF3,0xE7,0x80,0x1E,0xF0,0xF6,0x66,0xCC,0x0C,0xCC,0xCC,0x60,0x0C,
0x01,0x8F,0x0F,0xC3,0xE6,0x00,0x7C,0x39,0xCE,0x6C,0xFC,0x0C,0xCC,0xCC,0x70,0x1C,
0x00,0x8F,0x03,0x03,0xE4,0x00,0xE0,0x0C,0x84,0x7C,0xCC,0xF8,0x80,0x04,0x3F,0xF8,
0x06,0x0F,0x00,0x03,0xE1,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0x0F,0x00,0x03,0xC3,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xE3,0x00,0x03,0x1F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xF1,0x00,0x02,0x3F,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,
0x07,0xFE,0x00,0x01,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFF,0x00,0x03,0xFF,0x81,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,
0x01,0xFF,0xC0,0x0F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x7F,0xE0,0x1F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1F,0xF8,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//------------------------------main--------------------------------------------
void main( void )
{
CLK_CKDIVR=0X00;
delay(100);
GPIO_init();
LCD_init();
LCD_pic(dis);
}
//------------------------------functions---------------------------------------
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
void GPIO_init(void)
{
//LCD CS DIO CLK PSB
PF_DDR |=0X39;
PF_CR1 |=0X39;
PF_CR2 &=0XC6;
PF_ODR &=0XC6;
LCDPSB=0;
LCDCS=1;
}
void LCD_write(u8 data,u8 cmd_flag)
{
if(cmd_flag)
LCD_send_byte(0XFA);
else
LCD_send_byte(0XF8);
LCD_send_byte(data & 0XF0);
LCD_send_byte((data<<4) & 0XF0);
}
void LCD_init(void)
{
LCD_write(0X0C,0);
delay(1);
LCD_write(0X34,0);
delay(1);
LCD_write(0X01,0);
delay(1);
}
void LCD_pic(u8 *pic)
{
u8 i,j,k;
LCD_write(0X34,0);delay(10);
for ( i = 0; i <2; i++)
{
for ( j = 0; j <32; j++)
{
LCD_write(0X80+j,0);
LCD_write(0X80+8*i,0);
for ( k = 0; k <16; k++)
{
LCD_write(*pic++,1);
}
}
}
LCD_write(0X36,0);delay(10);
LCD_write(0X30,0);delay(10);
}
void LCD_send_byte(u8 byte)
{
for (u8 i = 0; i < 8; i++)
{
LCDCLK=0;
if ((byte<<i) & 0X80)
LCDDIO=1;
else
LCDDIO=0;
LCDCLK=1;
}
}
线反转式键盘扫描
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDRS PF_ODR_ODR0
#define LCDRW PF_ODR_ODR3
#define LCDEN PF_ODR_ODR4
#define LCDDATA PB_ODR
#define KeyPortIn PC_IDR
#define KeyPortOut PC_ODR
//------------------------------function declare--------------------------------
void GPIO_init(void);
void delay(u16 count);
void LCD_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_display(void);
void LCD_lr_DIS(u8 line,u8 row,u8 z);
u8 KEY_scan(void);
//------------------------------------------------------------------------------
u8 line1[]="==4*4 Keyboard==";
u8 line2[]="[Keynum]: ";
u8 table3[]={'0','1','2','3','4','5','6','7','8','9'};
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
LCD_init();
LCD_display();
u8 keydown;
while (1)
{
keydown=KEY_scan();
if(keydown!=0XFF)
{
LCD_lr_DIS(2,12,table3[keydown/10]);
LCD_lr_DIS(2,13,table3[keydown%10]);
delay(300);
}
LCD_lr_DIS(2,12,'N');
LCD_lr_DIS(2,13,'o');
}
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PF_DDR=0XFF;
PF_CR1=0XFF;
PF_CR2=0X00;
PF_ODR=0X00;
PB_DDR=0XFF;
PB_CR1=0XFF;
PB_CR2=0X00;
PB_ODR=0X00;
PC_DDR=0XF0;
PC_CR1=0XFF;
PC_CR2=0X0F;
PC_ODR=0X00;
}
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<50;j++);
}
}
void LCD_init(void)
{
LCD_write(0X38,0);
LCD_write(0X0E,0);
LCD_write(0X06,0);
LCD_write(0X01,0);
}
void LCD_write(u8 data,u8 cmd_flag)
{
LCDRS=cmd_flag;
LCDRW=0;
LCDEN=1;
delay(1);
LCDDATA=data;
LCDEN=0;
delay(1);
}
void LCD_display(void)
{
LCD_write(0X80,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line1[i],1);
}
LCD_write(0XC0,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line2[i],1);
}
}
void LCD_lr_DIS(u8 line,u8 row,u8 z)
{
switch (line)
{
case 1:
LCD_write(0X80+row,0);
LCD_write(z,1);
break;
case 2:
LCD_write(0XC0+row,0);
LCD_write(z,1);
break;
default:
break;
}
}
u8 KEY_scan(void)
{
PC_DDR=0XF0;
PC_CR1=0XFF;
PC_CR2=0X00;
KeyPortOut=0X0F;
u8 keypress=0XFF;
if(KeyPortIn != 0X0F)
{
delay(10);
switch (KeyPortIn)
{
case 0X0E:keypress=0;break;
case 0X0D:keypress=1;break;
case 0X0B:keypress=2;break;
case 0X07:keypress=3;break;
default:keypress=0XFF;break;
};
PC_DDR=0X0F;
PC_CR1=0XFF;
PC_CR2=0X00;
KeyPortOut=0XF0;
if(KeyPortIn!=0XF0)
{
delay(10);
switch (KeyPortIn)
{
case 0XE0:keypress +=0;break;
case 0XD0:keypress +=4;break;
case 0XB0:keypress +=8;break;
case 0X70:keypress +=12;break;
default:keypress=0XFF;break;
};
while(KeyPortIn!=0XF0);//按键保持
}
}
return keypress;
}
时钟变换
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LED PI_ODR_ODR0
//------------------------------function declare--------------------------------
void delay(u8 count);
//------------------------------global------------------------------------------
//------------------------------main--------------------------------------------
void main( void )
{
PI_DDR_DDR0=1;
PI_CR1_C10=1;
PI_CR2_C20=0;
while (1)
{
CLK_CKDIVR=0X00;
LED=0;
delay(100);
LED=1;
delay(100);
CLK_CKDIVR=0X08;
LED=0;
delay(100);
LED=1;
delay(100);
CLK_CKDIVR=0X10;
LED=0;
delay(100);
LED=1;
delay(100);
CLK_CKDIVR=0X18;
LED=0;
delay(100);
LED=1;
delay(100);
}
}
//------------------------------functions---------------------------------------
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
TIM4定时器
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LED PI_ODR_ODR0
//------------------------------function declare--------------------------------
void delay(u16 count);
void GPIO_init(void);
void TIM4_init(void);
//------------------------------global------------------------------------------
u16 num;
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
TIM4_init();
asm("rim");
TIM4_CR1 |=0X01;
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PI_DDR_DDR0=1;
PI_CR1_C10=1;
PI_CR2_C20=0;
LED=1;
}
void TIM4_init(void)
{
TIM4_PSCR=0X03;
TIM4_ARR=0XFA;
TIM4_IER=0X01;
TIM4_CNTR=0X00;
}
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
//------------------------------interrupt---------------------------------------
#pragma vector=0X19
__interrupt void EXTI_TIM4_IRQHandler(void)
{
num++;
TIM4_SR=0;
if (num==500)
{
LED=!LED;
num=0;
}
}
TIM1计时
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LED PI_ODR_ODR0
#define LED1 PI_ODR_ODR1
#define LED2 PI_ODR_ODR2
//------------------------------function declare--------------------------------
void delay(u16 count);
void GPIO_init(void);
//------------------------------global------------------------------------------
u8 keydown;
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
asm("sim");
EXTI_CR1=0X20;
asm("rim");
while(1)
{
if (keydown)
{
asm("sim");
//TIM1_set
keydown=0;
asm("rim")
}
}
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PC_DDR = 0X00;
PC_CR1= 0XFF;
PC_CR2 = 0XFF;
PI_DDR_DDR0=1;
PI_CR1_C10=1;
PI_CR2_C20=0;
PI_DDR_DDR1=1;
PI_CR1_C11=1;
PI_CR2_C21=0;
PI_DDR_DDR2=1;
PI_CR1_C12=1;
PI_CR2_C22=0;
PI_ODR=0XFF;
}
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
//------------------------------interrupt---------------------------------------
#pragma vector=0X07
__interrupt void EXTI_PORTC_IRQHandler(void)
{
delay(10);
switch (PC_IDR|0XF8)
{
case 0XFE:
keydown=1;
break;
case 0XFD:
keydown=2;
break;
case 0XFB:
keydown=3;
break;
default:
break;
}
while((PC_IDR|0XF8) !=0XFF);
}
TIM1 外部计数模式1
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
#define LCDRS PF_ODR_ODR0
#define LCDRW PF_ODR_ODR3
#define LCDEN PF_ODR_ODR4
#define LCDDATA PB_ODR
//------------------------------function declare--------------------------------
void GPIO_init(void);
void delay(u8 count);
void LCD_init(void);
void LCD_write(u8 data,u8 cmd_flag);
void LCD_display(void);
void LCD_lrdisplay(u8 line,u8 row,u8 data);
void num_display(u16 ch1_count);
void TIM1_CH1_SET(void);
//------------------------------------------------------------------------------
u8 line1[]="==MODE 1 Count==";
u8 line2[]="NuM: ";
u16 ch1_count;
u8 ch1_count_todisplay[5];
u8 num_table[]={'0','1','2','3','4','5','6','7','8','9'};
//------------------------------main--------------------------------------------
void main( void )
{
GPIO_init();
LCD_init();
LCD_display();
asm("sim");
TIM1_CH1_SET();
asm("rim");
while (1)
{
ch1_count=TIM1_CNTRH;
ch1_count= (ch1_count <<8)|TIM1_CNTRL;
num_display(ch1_count);
}
}
//------------------------------functions---------------------------------------
void GPIO_init(void)
{
PF_DDR=0XFF;
PF_CR1=0XFF;
PF_CR2=0X00;
PF_ODR=0X00;
PB_DDR=0XFF;
PB_CR1=0XFF;
PB_CR2=0X00;
PB_ODR=0X00;
//把TIM1的CH1对应GPIO PC1初始化好
PC_DDR_DDR0=0;
PC_CR1_C10=1;
PC_CR2_C20=0;
}
void delay(u8 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<50;j++);
}
}
void LCD_init(void)
{
LCD_write(0X38,0);
LCD_write(0X0D,0);
LCD_write(0X06,0);
LCD_write(0X01,0);
}
void LCD_write(u8 data,u8 cmd_flag)
{
LCDRS=cmd_flag;
LCDRW=0;
LCDEN=1;
delay(1);
LCDDATA=data;
LCDEN=0;
}
void LCD_display(void)
{
LCD_write(0X80,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line1[i],1);
}
LCD_write(0XC0,0);
for (u8 i = 0; i < 16; i++)
{
LCD_write(line2[i],1);
}
}
void LCD_lrdisplay(u8 line,u8 row,u8 data)
{
if(line==1) LCD_write(0X80+row,0);
if(line==2) LCD_write(0XC0+row,0);
LCD_write(data,1);
}
void num_display(u16 ch1_count)
{
ch1_count_todisplay[0]=ch1_count/10000;
ch1_count_todisplay[1]=(ch1_count%10000)/1000;
ch1_count_todisplay[2]=(ch1_count%1000)/100;
ch1_count_todisplay[3]=(ch1_count%100)/10;
ch1_count_todisplay[4]=ch1_count%10;
LCD_lrdisplay(2,4,num_table[ch1_count_todisplay[0]]);
LCD_lrdisplay(2,5,num_table[ch1_count_todisplay[1]]);
LCD_lrdisplay(2,6,num_table[ch1_count_todisplay[2]]);
LCD_lrdisplay(2,7,num_table[ch1_count_todisplay[3]]);
LCD_lrdisplay(2,8,num_table[ch1_count_todisplay[4]]);
}
void TIM1_CH1_SET(void)
{
TIM1_CCMR1=0X31;
TIM1_CCER1=0X02;
TIM1_SMCR=0X57;
TIM1_CR1 |=0X01;
}
操作串口UART1发送
//------------head----------------------
#include "iostm8s208mb.h"
//------------common-----------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------marco------------------------
#define LED PI_ODR
//------------funcs------------------------
void delay(u16 count);
void GPIO_init(void);
void uart1_sendByte(u8 data);
void uart1_send_line(char line[]);
//------------global----------------------
char line1[]="*****************************";
char *read;
//------------main------------------------
void main( void )
{
uart1_send_line(line1);
}
//------------funcs------------------------
void delay(u16 count)
{
u8 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<20;j++);
}
}
void GPIO_init(void)
{
PI_DDR |=0X0F;
PI_CR1 |=0X0F;
PI_CR2 &=0XF0;
PI_ODR |=0X0F;
}
void uart1_sendByte(u8 data)
{
UART1_SR &= 0XBF;
UART1_CR1=0X00;
UART1_CR3=0X00;
UART1_BRR2=0X00;
UART1_BRR1=0X0D;
UART1_CR2=0X48;
UART1_DR=data;
while(!(UART1_SR &0X40));
UART1_SR &= 0XBF;
}
void uart1_send_line(char line[])
{
read=line;
while(*read !='\0')
{
uart1_sendByte(*read);
read++;
}
uart1_sendByte('\r');
uart1_sendByte('\n');
}
//---------------interrupt-----------------
//-------------------------------------
操作串口UART1发送和接收
//------------------------------header------------------------------------------
#include "iostm8s208mb.h"
#include "stdio.h"
//------------------------------common datatype---------------------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------------------------macros------------------------------------------
//------------------------------function declare--------------------------------
void delay(u16 count);
void URAT1_Init(void);
void UART1_SendByte(u8 data);
int putchar(int ch);
char recive_data;
//------------------------------------------------------------------------------
//------------------------------main--------------------------------------------
void main( void )
{
CLK_CKDIVR=0X00;
delay(10);
URAT1_Init();
printf("我是谁,我在哪??\r\n");
asm("rim");
}
//------------------------------functions---------------------------------------
void URAT1_Init(void)
{
UART1_CR1=0X00;
UART1_CR3=0X00;
UART1_BRR2=0X02;
UART1_BRR1=0X68;
UART1_CR2=0X6C;
}
void UART1_SendByte(u8 data)
{
UART1_SR &=0XBF;
UART1_DR =data;
while(!(UART1_SR & 0X40));
UART1_SR &=0XBF;
}
int putchar(int ch)
{
while((UART1_SR & 0X80)==0X00);
UART1_SendByte((u8)ch);
return (ch);
}
void delay(u16 count)
{
u16 i,j;
while (count--)
{
for(i=0;i<50;i++)
for(j=0;j<50;j++);
}
}
#pragma vector=0X14
__interrupt void UART1_RX_IRQHandler(void)
{
char recive;
UART1_SR &=0XDF;
recive=UART1_DR;
recive_data=recive;
printf("收到了:");
UART1_SendByte(recive);
printf("\r\n");
}
获取18b20有无
//------------head----------------------
#include "iostm8s208mb.h"
#include "stdio.h"
//------------common-----------------------
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//------------marco------------------------
//------------funcs------------------------
void delay(u16 count);
void uart1_init(void);
void uart1_sendByte(u8 data);
int putchar(int ch);
void Welcome_Msg(void);
void DS18B20_reset(void);
void DS18B20_IO(u8 ds18b20_io);
//------------global----------------------
char give_to_stm8;
//------------main------------------------
void main( void )
{
CLK_CKDIVR=0x00;
delay(10);
uart1_init();
Welcome_Msg();
asm("rim");
while (1);
}
//------------funcs------------------------
void delay(u16 Count)
{
do{ }//直到型循环执行空操作
while(Count--);//Count形参控制执行次数影响延时时间
}
void uart1_init(void)
{
UART1_CR1=0X00;
UART1_CR3=0X00;
UART1_BRR2 = 0x02;
UART1_BRR1 = 0x68;
UART1_CR2=0X6C;
}
void uart1_sendByte(u8 data)
{
UART1_SR &= 0XBF;
UART1_DR=data;
while(!(UART1_SR &0X40));
UART1_SR &= 0XBF;
}
int putchar(int ch)
{
while((UART1_SR & 0X80)==0x00);
uart1_sendByte((u8)ch);
return (ch);
}
void Welcome_Msg(void)
{
printf("*************************");
printf("\r\n");
printf("choose your cmd pls!");
printf("\r\n");
printf("1-Do U have any DS18B20?");
printf("\r\n");
printf("2-read the DS18B20'S TMP!");
printf("\r\n");
printf("*************************");
printf("\r\n");
}
//---------------DS18B20-------------------
void DS18B20_IO(u8 ds18b20_io)
{
if (ds18b20_io)
{
PI_DDR_DDR4=1;
PI_CR1_C14=1;
PI_CR2_C24=0;
}
else
{
PI_DDR_DDR4=0;
PI_CR1_C14=1;
PI_CR2_C24=0;
}
}
void DS18B20_reset(void)
{
u8 havenoDS=0,haveflag=0;
u16 count=700;
DS18B20_IO(1);
PI_ODR_ODR4=1;
delay(1700);
PI_ODR_ODR4=0;
delay(1700);
PI_ODR_ODR4=1;
DS18B20_IO(0);
delay(65);
havenoDS=PI_IDR_IDR4;
printf("I'm scaning~\r\n");
while (count--)
{
printf("havenoDS:%c,haveflag:%c\r\n",havenoDS,haveflag);
if (!havenoDS) haveflag=1;
if (haveflag) break;
}
if (haveflag)
{
printf("Find one DS18B20~\r\n");
}
else
{
printf("I got nothing!\r\n");
}
delay(1150);
}
//---------------interrupt-----------------
#pragma vector=0X14
__interrupt void UART1_RX_IRQHandler(void)
{
char rev_data;
UART1_SR &=0XDF;
rev_data = UART1_DR;
give_to_stm8=rev_data;
switch (give_to_stm8)
{
case '1':
printf("I'll try to scan if I have any DS18B20 or not!\r\n");
//read 18b20's ROM;
DS18B20_reset();
printf("All done!Now I'm Back!\r\n");
give_to_stm8=0;
Welcome_Msg();
break;
case '2':
printf("I'll read DS18B20'S Temp:\r\n");
//read 18b20's Temp;
printf("All done!Now I'm Back!\r\n");
give_to_stm8=0;
Welcome_Msg();
case 0:
break;
default:
printf("wrong cmd,retry!\r\n");
give_to_stm8=0;
Welcome_Msg();
break;
}
}
//-------------------------------------