Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -----------------------------------------------------------------------------
- * File: servo.c
- * Project: Servo Motor Example
- * Author: Leandro Schwarz
- * Last modified: 28/08/2014
- * -------------------------------------------------------------------------- */
- // System definitions
- #define F_CPU 16000000UL
- #define MCU_8_BITS
- // Header files
- #include "LS_defines.h"
- #include "LS_ATmega328.h"
- // Constant definitions
- #define INC 10
- #define LEFT 0
- #define RIGHT 1
- // Function declaration
- // Global variables
- // main function
- int main()
- {
- uint16 value = 1000;
- uint8 direction = LEFT;
- // Port configuration
- setBit(DDRB, PB1);
- // Timer1 configuration
- timer1ClockPrescaller8();
- timer1PhaseFrequencyCorrectPWMICR1Mode();
- timer1OC1ANonInvertedMode();
- timer1SetInputCaptureValue(20000);
- while(1){
- if(direction == LEFT){
- value += INC;
- if(value >= 2000){
- value = 2000;
- direction = RIGHT;
- }
- }else{
- value -= INC;
- if(value <= 1000){
- value = 1000;
- direction = LEFT;
- }
- }
- timer1SetCompareAValue(value);
- _delay_ms(30);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment