C Program Bit manipulation macros Example

How to write a C Program Bit manipulation macros in C Programming Language ?


  1. //Bit manipulation macros
  2. #define BIT_SET(value,bitno)    ( (value) |= (1<<(bitno)))
  3. #define BIT_CLEAR(value,bitno)  ( (value) &= ~(1<<(bitno)))
  4.  
  5. #include <avr/io.h>
  6. #include <stdio.h>
  7. #include "USART.h"
  8. #include <util/delay.h>
  9. #include "ADC.h"
  10.  
  11. FILE fpUSART = FDEV_SETUP_STREAM(USARTSendByte, USARTGetByte, _FDEV_SETUP_RW);
  12.  
  13. // Sets up Timer 1 for fast-PWM operation
  14. // DC-Motor Connected to PB0 and OSC1A (PB1)
  15. void init_PWM_OSC1A()
  16. {
  17.         TCCR1A = (1<<COM1A1) | (1<<WGM11);     // Clear on compare, fast PWM, TOP=ICR1 (WGM13/WGM12 in TCCR1B)
  18.         TCCR1B = (1<<WGM12)  | (1<<WGM13) ;                   // CTC-mode width ICR1 defines the TOP'value
  19.         TCCR1B |=(1<< CS11)  | (1<<CS10);                     // Prescaler 64
  20.  
  21.         ICR1 = 5000;    //- Input Capture Register In PWM mode this register defines the TOP period
  22.         OCR1A = 0;      // Start width no pulse
  23. }
  24.  
  25. void rotateClockwise(int Speed)
  26. {
  27.         //Clear OC1A on Compare Match,
  28.         //set OC1A at BOTTOM (non-inverting mode)
  29.         BIT_CLEAR(TCCR1A,COM1A0);
  30.         BIT_SET(TCCR1A,COM1A1);
  31.  
  32.         BIT_CLEAR(PORTB,PB0);  //Clear bit 0 PORTB (PB0)
  33.         OCR1A=Speed;
  34. }
  35.  
  36. void rotateCounterClockwise(int Speed)
  37. {
  38.         //Clear OC1A on Compare Match,
  39.         //set OC1A at BOTTOM (non-inverting mode)
  40.         BIT_CLEAR(TCCR1A,COM1A0);
  41.         BIT_SET(TCCR1A,COM1A1);
  42.  
  43.         BIT_SET(PORTB,PB0);  //Clear bit 0 PORTB (PB0)
  44.         OCR1A=5000-Speed;
  45. }
  46.  
  47. // A DC Motor is connected to PB0 and OSC1A (PB1)
  48. // DIGITAL-8 and DIGITAL-9 on the Arduino Uno Board
  49. // The program rotates the motor slowly clockwise
  50. int main(void)
  51. {
  52.         int ADCverdi;
  53.         int fart;
  54.         init_PWM_OSC1A();
  55.         init_USART();
  56.         InitADC();
  57.         stdin=&fpUSART;
  58.         stdout=&fpUSART;
  59.         DDRB =  (1 << PB1) | (1<<PB0);
  60.  
  61.         DDRC = (1 << PC0) | (1 << PC2);
  62.         PORTC &= ~(1<<PC2);
  63.         PORTC = (1<<PC0);
  64.  
  65.     while(1)
  66.         {
  67.                 ADCverdi = ReadADC(1);
  68.                 _delay_ms(50);
  69.                 if (ADCverdi < 500)
  70.                 {
  71.                         fart = (500 - ADCverdi)*9;
  72.                         rotateClockwise(fart);
  73.                 }
  74.                 if (ADCverdi > 520)
  75.                 {
  76.                         fart = (ADCverdi - 520)*9;
  77.                         rotateCounterClockwise(fart);
  78.                 }
  79.                 if(ADCverdi > 500 && ADCverdi < 520)
  80.                 {
  81.                         rotateClockwise(0);
  82.                 }
  83.                 printf("%d \n\r", fart);
  84.         }
  85. }


Learn More :