初级会员
- 积分
- 106
- 金钱
- 106
- 注册时间
- 2016-7-23
- 在线时间
- 13 小时
|
楼主 |
发表于 2016-8-3 09:22:57
|
显示全部楼层
完整实例里面有可参考部分
/****************目前已经可以通过手势操控电机方向**********************/
/***********PB高四位接电机,PB1、PB3接红外对管**************************************************/
/*******串口发送0,电机顺时针转动,发送1,逆时针********/
#include"stm8s.h"//头
int i,j,m,n,p,q,temp;
u16 x;
unsigned char CCW[8]={0x80,0xc0,0x04,0x60,0x20,0x30,0x10,0x90}; //
unsigned char CW[8]={0x90,0x10,0x30,0x20,0x60,0x04,0xc0,0x80};//正时钟旋转相序表
int box[2];
void UART2Conf(void);
void USART_SendByte(u8 Data);
void USART_Write(u8*data,u16 len);
void GPIO_configuration();
void judge();
int OneValue(ADC1_Channel_TypeDef ADC1_Channel,ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChannel);
void delay(u32 i)
{
u32 j;
for(j=0;j<i;j++)
{;}
}
void main()
{
GPIO_configuration();
UART2Conf();
while(1)
{
//ADC1_DeInit();
//judge();
while(p)
{
for(int i=0;i<8;i++)
{//GPIO_Write(GPIOD, CW[i]);
GPIO_Write(GPIOB, CCW[i]);
delay(300);
}//慢速正转
USART_Write("顺时针转动方向\n",16);
judge();
if(p == 0)
break;
}
//judge();
while(q){
for(int i=0;i<8;i++)
{//GPIO_Write(GPIOD, CW[i]);
GPIO_Write(GPIOB, CW[i]);
delay(300);
}//慢速正转
USART_Write("逆时针转动方向\n",16);
judge();
if(q == 0)
break;
}
}
}
void judge(){
i=OneValue(0x01,0x01);
box[0] = i;
delay(10); //两次之间好像必须有延时 不然会错误 有待考证
j=OneValue(0x03,0x03);
box[1] = j;
delay(10);
if(i <= 500 && j > 500){
while(1){
temp = OneValue(0x01,0x01);
delay(10);
if(temp <= 500){}
else
break;
}
while(1){
m=OneValue(0x01,0x01);
delay(10); //两次之间好像必须有延时 不然会错误 有待考证
n=OneValue(0x03,0x03);
delay(10);
if(m > 500 && n <= 500){
p = 1;
q = 0;
break;
}
}
}
if(i > 500 && j <= 500){
while(1){
temp = OneValue(0x03,0x03);
delay(10);
if(temp <= 500){}
else
break;
}
while(1){
m=OneValue(0x01,0x01);
delay(10); //两次之间好像必须有延时 不然会错误 有待考证
n=OneValue(0x03,0x03);
delay(10);
if(m <= 500 && n > 500){
p = 0;
q = 1;
break;
}
}
}
}
void GPIO_configuration()
{
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC,GPIO_PIN_LNIB,GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_DeInit(GPIOB);
GPIO_Init(GPIOB, GPIO_PIN_HNIB, GPIO_MODE_OUT_PP_LOW_FAST);
}
void UART2Conf(void)
{
UART2_DeInit();
UART2_Init(9600 ,
UART2_WORDLENGTH_8D,
UART2_STOPBITS_1 ,
UART2_PARITY_NO,
UART2_SYNCMODE_CLOCK_DISABLE,
UART2_MODE_TXRX_ENABLE);
UART2_ITConfig(UART2_IT_RXNE,ENABLE);
UART2_Cmd( ENABLE);
enableInterrupts();
}
#pragma vector=23
__interrupt void EXIT_UART2_IRQHander(void)//中断函数
{
x=UART2_ReceiveData8();
if(x == 0){
p = 1;
q = 0;
}
if(x == 1){
p = 0;
q = 1;
}
UART2_ClearITPendingBit(UART2_IT_RXNE);
}
void USART_SendByte(u8 Data)
{
while(!UART2_GetFlagStatus(UART2_FLAG_TXE));//发送缓冲区空闲标志位
UART2_SendData8(Data);
}
void USART_Write(u8*data,u16 len)
{
u16 i;
for(i=0;i<len;i++)
{
while(!UART2_GetFlagStatus(UART2_FLAG_TXE));//发送缓冲区空闲标志位
UART2_SendData8(data[i]);
}
}
int OneValue(ADC1_Channel_TypeDef ADC1_Channel,ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChannel)
{
unsigned int ADConversion_Value;
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS , ADC1_Channel, ADC1_PRESSEL_FCPU_D18,\
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SchmittTriggerChannel,DISABLE);
ADC1_Cmd(ENABLE);
ADC1_StartConversion();
ADConversion_Value = ADC1_GetConversionValue();
return ADConversion_Value;
} |
|