/******************************************
* *
* *
* *
* *
*******************************************/
#include<pthread.h>
#include"gpio.h"
#include"delay.h"
#include"typedef.h"
#define LED_PIN1
#define PWM_PIN0
#define SIGNAL_CYCLE 200//uS
pthread_t ntid_pwm0;
volatile unsigned char duty0;
voidGenerateOnePuls(intfd,bool High,float duty)
{
inttimeLow;
inttimeHigh;
if(duty >=100)duty =100;
timeHigh = (int)(duty/100*SIGNAL_CYCLE);
timeLow = SIGNAL_CYCLE - timeHigh;
if(!High)
{
timeHigh = timeLow;
timeLow = SIGNAL_CYCLE - timeHigh;
}
SetGpio(fd,HIGH);
delay_us(timeHigh);
SetGpio(fd,LOW);
delay_us(timeLow);
}
void* Pwm0_thread_fun(void* arg)
{
intfd_pin_pwm0;
intfd_mode_pwm0;
GpoiInitial(&fd_pin_pwm0,&fd_mode_pwm0,PWM_PIN,OUTPUT);
SetGpio(fd_pin_pwm0,LOW);
while(1)
{
GenerateOnePuls(fd_pin_pwm0,TRUE,duty0);
}
return(void*)0;
}
intmain(intargc,char **argv)
{
interr;
BOOL led_on;
intfd_pin_led;
intfd_mode_led;
duty0 =1;
GpoiInitial(&fd_pin_led,&fd_mode_led,LED_PIN,OUTPUT);
err = pthread_create(&ntid_pwm0, NULL, Pwm0_thread_fun, NULL);
if(err !=0)
{
printf("%s\n", strerror(err));
}
while(1)
{
delay_ms(100);
duty0 +=1;
if(duty0 >=100)duty0 =1;
led_on = !led_on;
SetGpio(fd_pin_led,led_on);
}
return1;
}