操作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 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++); } } //------------------------------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 != '