步进电机
#include “motor.h”
#include “delay.h”
#define uint unsigned int
uint16_t phasecw[4] ={0x18,0x30,0x60,0x48};// D-C-B-A ¶þÏàÀø´Å·¨ AB->BC->CD->DA
uint16_t phaseccw[8]={0x07,0x03,0x0B,0x09,0x0D,0x0C,0x0E,0x06};// A-B-C-D.
//uint16_t phasecw[4] ={0x01,0x02,0x04,0x08};// D-C-B-A
//uint16_t phasecw[4] ={0x03,0x06,0x0C,0x09};// D-C-B-A
//uint16_t phaseccw[4]={0x03,0x09,0x0C,0x06};// A-B-C-D.
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
void Moto_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_3 | GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6 );
}
void Motorcw(int speed)//²½½øµç»ú˳ʱÕëת
{
uint8_t i,j;
for(i=0;i<4;i++)
{
for(j=3;j<=6;j++)
{
if( ((phasecw[i]>>j)&0x01) == 0) GPIO_ResetBits(GPIOA,1<<j);
else GPIO_SetBits(GPIOA,1<<j);
}
//GPIO_Write(GPIOA,phasecw[i]);
delay_ms(speed);
// Delay_xms(speed);
}
}
void Motorccw(int speed) //²½½øµç»úÄæʱÕëת
{
uint8_t i;
for(i=0;i<8;i++)
{
GPIO_Write(GPIOA,phaseccw[i]);
delay_ms(speed);
}
}
void MotorStop(void) //²½½øµç»úÔÝÍ£
{
GPIO_Write(GPIOA,0x0000);
}
//ÓÉÓÚ Ò»¸öÂö³å Êä³öÖá ת0.08789¶È£¨µç»úʵת0.0878964=5.625¶È£©£¬¼´²½½ø½ÇΪ5.625¶È¡£
//ÔòתÍêA-B-C-DΪ 8¸öÂö³å £¬¼´0.087898=0.70312¶È¡£Èô³ÆA-B-C-DΪһ¸öÖÜÆÚ£¬ÔòjΪÐèÒªµÄתÍêangle½Ç¶ÈËùÐèµÄÖÜÆÚÊý¡£
void Motorcw_angle(int angle,int speed)//²½½øµç»úÄæʱÕëת £¬angle¿ØÖÆÐýת½Ç¶È£¬speed¿ØÖÆÐýתËٶȣ¨>500£©
{
int i,j;
j=(int)(angle/0.70312);
for(i=0;i<j;i++)
{
Motorcw(speed);
}
MotorStop();
}
void Motorccw_angle(int angle,int speed)//²½½øµç»úÄæʱÕëת £¬angle¿ØÖÆÐýת½Ç¶È£¬speed¿ØÖÆÐýתËٶȣ¨>500£©
{
int i,j;
j=(int)(angle/0.70312);
for(i=0;i<j;i++)
{
Motorccw(speed);
}
MotorStop();
}
**
LCD1602
**
#include “delay.h”
#include “stdio.h”
#include “LCD1602.h”
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF | RCC_AHB1Periph_GPIOE| RCC_AHB1Periph_GPIOG,ENABLE);//ʹÄÜPB,PD¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOE, &GPIO_InitStructure); //³õʼ»¯GPIOD0~7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOF, &GPIO_InitStructure); //³õʼ»¯GPIB15,14,13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //ÆÕͨÊä³öģʽ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ
GPIO_Init(GPIOG, &GPIO_InitStructure); //³õʼ»¯GPIB15,14,13
}
/* µÈ´ýÒº¾§×¼±¸ºÃ */
void LCD1602_Wait_Ready(void)
{
u8 sta;
DATAOUT(0xff);
LCD_RS_Clr();
LCD_RW_Set();
do
{
LCD_EN_Set();
delay_ms(5); //ÑÓʱ5ms£¬·Ç³£ÖØÒª
// sta = GPIO_ReadInputData(GPIOF);//¶Áȡ״̬×Ö
sta =GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_7);
LCD_EN_Clr();
}while(sta & 0x80);//bit7µÈÓÚ1±íʾҺ¾§Õý棬Öظ´¼ì²âÖ±µ½ÆäµÈÓÚ0Ϊֹ
}
/* ÏòLCD1602Òº¾§Ð´ÈëÒ»×Ö½ÚÃüÁcmd-´ýдÈëÃüÁîÖµ */
void LCD1602_Write_Cmd(u8 cmd)
{
LCD1602_Wait_Ready();
delay_ms(1);
LCD_RS_Clr();
delay_ms(1);
LCD_RW_Clr();
delay_ms(1);
DATAOUT(cmd);
delay_ms(1);
LCD_EN_Set();
delay_ms(1);
LCD_EN_Clr();
delay_ms(1);
//printf("%d",cmd);
}
/* ÏòLCD1602Òº¾§Ð´ÈëÒ»×Ö½ÚÊý¾Ý£¬dat-´ýдÈëÊý¾ÝÖµ */
void LCD1602_Write_Dat(u8 dat)
{
LCD1602_Wait_Ready();
delay_ms(1);
LCD_RS_Set();
delay_ms(1);
LCD_RW_Clr();
delay_ms(1);
DATAOUT(dat);
delay_ms(1);
LCD_EN_Set();
delay_ms(1);
LCD_EN_Clr();
delay_ms(1);
}
/* ÇåÆÁ */
void LCD1602_ClearScreen(void)
{
LCD1602_Write_Cmd(0x01);
}
/* ÉèÖÃÏÔʾRAMÆðʼµØÖ·£¬Ò༴¹â±êλÖã¬(x,y)-¶ÔÓ¦ÆÁÄ»ÉϵÄ×Ö·û×ø±ê */
void LCD1602_Set_Cursor(u8 x, u8 y)
{
u8 addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LCD1602_Write_Cmd(addr | 0x80);
}
/* ÔÚÒº¾§ÉÏÏÔʾ×Ö·û´®£¬(x,y)-¶ÔÓ¦ÆÁÄ»ÉϵÄÆðʼ×ø±ê£¬str-×Ö·û´®Ö¸Õë */
void LCD1602_Show_Str(u8 x, u8 y, u8 *str)
{
LCD1602_Set_Cursor(x, y);
while(*str != ‘\0’)
{
LCD1602_Write_Dat(*str++);
}
}
/* ³õʼ»¯1602Òº¾§ /
void LCD1602_Init(void)
{
GPIO_Configuration();
LCD1602_Write_Cmd(0x38); //162ÏÔʾ£¬5*7µãÕó£¬8λÊý¾Ý¿Ú
LCD1602_Write_Cmd(0x0c); //¿ªÏÔʾ£¬¹â±ê¹Ø±Õ
LCD1602_Write_Cmd(0x06); //ÎÄ×Ö²»¶¯£¬µØÖ·×Ô¶¯+1
LCD1602_Write_Cmd(0x01); //ÇåÆÁ
// LCD1602_Write_Cmd(0X02);
// LCD1602_Write_Cmd(0X06);
// LCD1602_Write_Cmd(0X00);
// LCD1602_Write_Cmd(0X0C);
// LCD1602_Write_Cmd(0X38);
// LCD1602_Write_Cmd(0x01);
}
**
语音播报
**
#include “sys.h”
#include “delay.h”
#include “string.h”
#include “Audio.h”
#define SPI_CS PCout(0)
#define SPI_SCK PDout(3)
#define SPI_SDO PDout(4)
static void SPI_Send_Data(uint8_t dat)
{
uint8_t i = 0;
SPI_CS = 0;
SPI_SCK = 0;
for (i = 0; i < 8; i++)
{
SPI_SDO = (dat & 0x01) ? 1 : 0;
SPI_SCK = 1;
delay_us(2);
SPI_SCK = 0;
dat >>= 1;
delay_us(1);
}
SPI_CS = 1;
delay_us(5);
}
static void SPI_WriteCMDData(uint8_t cmd, uint8_t dat)
{
SPI_Send_Data(cmd);
SPI_Send_Data(dat);
}
void Audio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3| GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
SPI_WriteCMDData(0x01, 0); //ÓïÑÔ 0ÖÐÎÄ 1Ó¢ÎÄ
SPI_WriteCMDData(0x02,50); //ËÙ¶È 1-100 10Õý³£ËÙ¶È
SPI_WriteCMDData(0x03,10); //Òôµ÷ 0-20 10Õý³£
SPI_WriteCMDData(0x04 ,0); //²¥·Åģʽ ĬÈÏ0£º²¥·ÅÇ°Ç¿ÖƽáÊøµ±Ç°µÄ²¥·Å 1£ºµþ¼Ó²¥·Å
}
void Audio_Start( uint8_t *str)
{
SPI_WriteCMDData(0x05, strlen(str)); //×Ö·û´®³¤¶È
SPI_Send_Data(0x06); //·¢ËÍÎı¾
while(*str)
{
SPI_Send_Data(*str++);
}
}
**
TFT
**
static void TFT_WR_REG(uint16_t cmd)
{
PEout(4)=0; //RW
PEout(5)=0; //RS
PGout(11)=0; //CS
GPIO_Write(GPIOF,cmd);
PGout(11)=1;
delay_us(1);
}
static void TFT_WR_Data(uint16_t dat)
{
PGout(11)=0;
PEout(5)=1;
PEout(4)=0;
GPIO_Write(GPIOF,dat);
PGout(11)=1;
delay_us(1);
}
//¿ª´°
static void TFT_Set_Window(uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY)
{
TFT_WR_REG(0x0020);
TFT_WR_Data(startX);
TFT_WR_REG(0x0021);
TFT_WR_Data(endX);
TFT_WR_REG(0x0022);
TFT_WR_Data(startY);
TFT_WR_REG(0x0023);
TFT_WR_Data(endY);
TFT_WR_REG(0x002F);
}
//ÉèÖùâ±êλÖÃ
static void TFT_SetCursor(uint16_t xCursor, uint16_t yCursor)
{
TFT_WR_REG(0x0024);
TFT_WR_Data(xCursor);
TFT_WR_REG(0x0025);
TFT_WR_Data(yCursor);
TFT_WR_REG(0x002F);
}
//»µã
static void TFT_DrawPoint(uint16_t x, uint16_t y, uint16_t color)
{
TFT_SetCursor(x,y);
TFT_WR_Data(color);
}
//ÇåÆÁ
void TFT320_Clear(uint16_t color)
{
uint16_t i, j;
TFT_Set_Window(0, 0, 319, 239);
for (i = 0; i < 320; i++)
{
for (j = 0; j < 240; j++)
{
TFT_WR_Data(color);
}
}
}
static void TFT_DisplayChar(uint16_t x, uint16_t y, uint8_t Font, uint8_t p)
{
register uint16_t i = 0, k = 0, j = p - ’ ', e = 0;
if ((p == '\r') || (p == '\n')) return;
if (Font == 16)
{
j <<= 4;
TFT_Set_Window(x, y, x + 7, y + 15);
for (i = j; i < (16 + j); i++)
{
e = zm8x16_Table[i];
for (k = 0; k < 8; k++)
{
if (e & (1 << k))
{
TFT_WR_Data(COLOR_WHITE);
}
else
{
TFT_WR_Data(COLOR_BLACK);
}
}
}
}
if (Font == 32)
{
j <<= 6;
TFT_Set_Window(x, y, x + 15, y + 31);
for (i = j; i < (64 + j); i += 2)
{
e = zm16x32_Table[i];
for (k = 0; k < 8; k++)
{
if (e & (1 << k))
{
TFT_WR_Data(COLOR_WHITE);
}
else
{
TFT_WR_Data(COLOR_BLACK);
}
}
e = zm16x32_Table[i + 1];
for (k = 0; k < 8; k++)
{
if (e & (1 << k))
{
TFT_WR_Data(COLOR_WHITE);
}
else
{
TFT_WR_Data(COLOR_BLACK);
}
}
}
}
}
void TFT_DisplayChars(uint16_t x, uint16_t y, uint8_t Font, char* p)
{
while (*p != ‘\0’)
{
if (*p == ‘\r’) {x = 0; p++; continue;}
if (*p == ‘\n’) {y += Font; p++; continue;}
TFT_DisplayChar(x, y, Font, *p++);
if (Font == 16)
{
x += 8;
if (x >= (320 - 8))
{
y += 16;
x = 0;
}
}
if (Font == 32)
{
x += 16;
if (x >= (320 - 16))
{
y += 32;
x = 0;
}
}
}
}
void TFT_Init(void)
{
uint16_t i = 0;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
TFT320_Clear(COLOR_WHITE);
for (i = 0; i < 319; i++)
{
TFT_DrawPoint(i, 150, COLOR_BLUE);
}
// TFT_DisplayChars(30, 179, 32, "zm8x16_Table[1]);
// TFT_DisplayChars(30, 9, 32, “0”);
}
**
直流电机
**
#include “sys.h”
#include “DCmotor.h”
void DCmotor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10| GPIO_Pin_11 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM4_PWM_Init(u32 arr,u32 psc)
{
//´Ë²¿·ÖÐèÊÖ¶¯ÐÞ¸ÄIO¿ÚÉèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM14ʱÖÓʹÄÜ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //ʹÄÜPORTFʱÖÓ
GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4); //GPIOF9¸´ÓÃΪ¶¨Ê±Æ÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //ËÙ¶È100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_Init(GPIOD,&GPIO_InitStructure); //³õʼ»¯PF9
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨Ê±Æ÷·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòÉϼÆÊýģʽ
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶¯ÖØ×°ÔØÖµ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);//³õʼ»¯¶¨Ê±Æ÷14
//³õʼ»¯TIM14 Channel1 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐÔµÍ
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM1 4OC1
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); //ʹÄÜTIM14ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
TIM_ARRPreloadConfig(TIM4,ENABLE);//ARPEʹÄÜ
TIM_Cmd(TIM4, ENABLE); //ʹÄÜTIM14
}
void DCMotorcw()
{
GPIO_SetBits(GPIOA,GPIO_Pin_10);
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
}
void DCMotorccw()
{
GPIO_ResetBits(GPIOA,GPIO_Pin_10);
GPIO_SetBits(GPIOA,GPIO_Pin_11);
}
void DCMotor_setSpeed(int16_t speed)
{
if(speed>=0) DCMotorcw();
else DCMotorccw();
TIM_SetCompare1(TIM4,speed);
}
#define COLOR_BLACK RGB888_TO_RGB565(0x000000) // 黑
#define COLOR_WHITE RGB888_TO_RGB565(0xFFFFFF) // 白
#define COLOR_YELLOW RGB888_TO_RGB565(0xFFFF00) // 黄
#define COLOR_RED RGB888_TO_RGB565(0xFF0000) // 红
#define COLOR_PURPLE RGB888_TO_RGB565(0xB23AEE) // 紫
#define COLOR_GREY RGB888_TO_RGB565(0xA8A8A8) // 灰
#define COLOR_BLUE RGB888_TO_RGB565(0x0000FF) // 蓝