I want to use PWM on Atmega640, but i found that the timer use 16 bit PWM, and i ussually use 10 bit PWM on Atmega16, The difference i got is the OCR got H and L on it..like OCR3BH and OCR3BL, so can anybody expalin how to control this 2 register like the one on Atmega16 ( only OCR3B ) ?
Here is my code ( but it didt work )
#include <mega640.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>
#define MAX 0xFFFF
void set_ports()
{
PORTE = 0b00000000;
DDRE = 0b11111111;
// ||||||||
// |||||||\___0:
// ||||||\____1:
// |||||\_____2: Status_1
// ||||\______3: Status_2
// |||\_______4: PWM_1
// ||\________5: PWM_2
// |\_________6: Status_3
// \__________7: Status_4
}
/*
Timer/Counter 3 initialization
Clock source: System Clock
Clock value: 16000.000 kHz
Mode: Ph. correct PWM top=03FFh
Noise Canceler: Off
Input Capture on Falling Edge
OC3A output: Discon.
OC3B output: Non-Inv.
OC3C output: Non-Inv.
Timer 3 Overflow Interrupt: Off
Input Capture Interrupt: Off
Compare A Match Interrupt: Off
Compare B Match Interrupt: Off
Compare C Match Interrupt: Off
*/
void set_timers()
{
TCCR3A = 0x14;
TCCR3B = 0x01;
TCNT3H = 0x00;
TCNT3L = 0x00;
ICR3H = 0x00;
ICR3L = 0x00;
OCR3AH = 0x00;
OCR3AL = 0x00;
OCR3BH = 0x00;
OCR3BL = 0x00;
OCR3CH = 0x00;
OCR3CL = 0x00;
}
/*LED status 3 x*/
void system_led()
{
int i;
for(i = 0; i <= 2; i++)
{
PORTB.6 = 1;
delay_ms(1000);
PORTB.6 = 0;
delay_ms(1000);
}
}
/*
Motor Control
Motor Kanan :
PWM_1 = PORTE.4 OCR3B
Status_1 = PORTE.2
Status_2 = PORTE.3
Truth Table :
Status_1 = 1, Status_2 = 0 > Motor Kanan Mundur
Status_1 = 0, Status_2 = 1 > Motor Kanan Maju
Motor Kiri :
PWM_2 = PORTE.5 OCR3C
Status_3 = PORTE.6
Status_4 = PORTE.7
Truth Table :
Status_3 = 1, Status_4 = 0 > Motor Kiri Mundur
Status_3 = 0, Status_4 = 1 > Motor Kiri Maju
*/
void motor_control(float duty_cycle_kanan, int status_1, int status_2, float duty_cycle_kiri, int status_3, int status_4)
{
OCR3BH = MAX*duty_cycle_kanan;
OCR3BL = MAX*duty_cycle_kanan;
PORTE.2 = status_1;
PORTE.3 = status_2;
OCR3CH = MAX*duty_cycle_kiri;
OCR3CL = MAX*duty_cycle_kiri;
PORTE.6 = status_3;
PORTE.7 = status_4;
}
/*Motor Brake*/
void brake(int status_1, int status_2, int status_3, int status_4)
{
PORTE.2 = status_1;
PORTE.3 = status_2;
PORTE.6 = status_3;
PORTE.7 = status_4;
}
void main()
{
set_ports();
set_timers();
system_led();
while(1)
{
motor_control(1,1,0,1,1,0);
delay_ms(2000);
motor_control(0.5,1,0,0.5,1,0);
delay_ms(2000);
brake(0,0,0,0);
delay_ms(1000);
motor_control(1,0,1,1,0,1);
delay_ms(2000);
motor_control(0.5,0,1,0.5,0,1);
delay_ms(2000);
brake(0,0,0,0);
delay_ms(1000);
}
}
I use this to control 2 DC motor using L298 motor driver, the motor manages to changing direction but the PWM pin can't give the output..anyone can help me with this type of PWM ?