2013年2月12日 星期二

DIY - 智能小車:L298N 電機驅動板模組程式 (十七)

DIY - 智能小車:L298N 電機驅動板模組程式 (十七) 

L298N 電機驅動板模組程式主要是驅動電機前後、停止和速度,前後是控制 IN1 IN2 的正負,停止是要 IN1 IN2 極性相同,控制速度是要改變 Duty Cycle,所以用了 16F877A CCP1 / CCP2 PWM 功能,而 PWM 的速度由 RA1 ADC (Analog to Digital Converter) 的可變電阻控制,衹要調節可變電阻便可以更改 PWM Duty Cycle,電機速度便可改變。

智能小車的 Top View
Duty Cycle = 0%
Duty Cycle = 69%
Duty Cycle = 100%
調節可變電阻
16F877A 調節 PWM 程式:
void main(void)
{
int16 adc_value;
int16 ratio;
float steps;
float volts;

lcd_init();

set_tris_b(0b11001111);
output_low(PIN_B4);
output_low(PIN_B5);

setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

set_pwm1_duty(0);
set_pwm2_duty(0);

setup_timer_2(T2_DIV_BY_16, 255, 1);

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(1);          // Set RA1 to ADC Channel 
delay_us(20);

while(TRUE)
  {

   output_bit(PIN_A0, 1);     // output logical 1 to PIN_A0
   adc_value = read_adc();
   steps = (float)(adc_value);
   ratio = (adc_value /10.23);
   volts = (float)(adc_value * 5)/1023.0;

   lcd_putc("f");           // Clear Screen before printing buffer  
   printf(lcd_putc, "f%sn","BWS ADC RA1 v1.0");
   printf(lcd_putc, "%4.0f %lu%s %3.2f%s",steps,ratio,"%",volts,"V");

   set_pwm1_duty(adc_value*0.392);        // Fixed the Left Speed
   set_pwm2_duty(adc_value*0.392);        // Fixed the Right Speed

   output_bit(PIN_A0, 0);     // output logical 0 to PIN_A0
   delay_ms(100);
  }
} // End main

相關網址:
※ DIY - 智能小車:ST L298N 電機驅動集成電路介紹 (十三)
※ DIY - 智能小車:L298N 電機驅動板模組 (十四)
※ DIY - 智能小車:L298N 電機驅動板模組邏輯電路 (十五)
※ DIY - 智能小車:L298N 電機驅動板模組組裝 (十六)
※ DIY - 智能小車:L298N 電機驅動板模組程式 (十七)

2013 年 2月 12日 天氣報告
氣溫:16.9@ 22:10
相對濕度:百分之 65%
天氣:大致多雲

沒有留言:

張貼留言