Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #define F_CPU 16000000UL
  2.  
  3. // the number of cycles it does after one loop is 10
  4. #define CYCLES  10
  5.  
  6. // 1 tick at 16mhz = 0,0000000625. 1/f_cpu.  
  7. // RESOLUTION = F_CPU / CYCLES /‭ 16777215‬ 24bit
  8. #define RESOLUTION  0.095367431640625
  9.  
  10.  
  11.  
  12. volatile uint8_t runstate;      // indicate on/off      
  13. volatile uint8_t signal;        // signal type
  14. volatile double frequency;    //frequency value
  15. volatile uint32_t acc;
  16.  
  17.  
  18. const uint8_t sinewave[] PROGMEM = { 0x80,0x83,0x86,... }
  19. const uint8_t *SIGNALS[] = { sinewave, .... };
  20.  
  21. inline void static signalOutOLD(const uint8_t *signal, uint8_t ad2, uint8_t ad1, uint8_t ad0)
  22. {
  23.     asm volatile(  
  24.         "eor r18, r18   ;r18<-0"
  25.         "eor r19, r19   ;r19<-0"
  26.         "1:"
  27.         "add r18, %0    ;1 cycle, (ad0) variable to r18"
  28.         "adc r19, %1    ;1 cycle, (ad1) variable to r19"
  29.         "adc %A3, %2    ;1 cycle, (ad2) variable to r30"
  30.         "lpm            ;3 cycles, load byte to r0 from Z pointed"
  31.         "out %4, r0     ;1 cycle, Output data value on r0 to PortD"
  32.         "sbis %5, 2     ;1 cycle if PB2 is set skip the jump."
  33.         "rjmp 1b        ;2 cycles, jump to 1: in unix style. 10 cycles tot"
  34.         :
  35.         :"r" (ad0),"r" (ad1),"r" (ad2),"e" (signal),"I" (_SFR_IO_ADDR(PORTD)), "I" (_SFR_IO_ADDR(PORTB))
  36.         :"r18", "r19"
  37.     );
  38. }
  39.  
  40. // function is called from ISR(SPI_STC_vect) when the start|stop command is received.
  41. void set_runstate(void) {
  42.     // toggle the state.
  43.     runstate = !runstate;
  44.     BITS_FLIP(PORTB, PB2);
  45. }
  46.  
  47. int main(void) {
  48.  
  49.     // test led light1,
  50.     BITS_SET(DDRB, PB1);
  51.     BITS_CLEAR(PORTB, PB1);
  52.  
  53.     // asm stop bit.  a led is connected to this port to indicate when the bit is set.
  54.     BITS_SET(DDRB, PB2);
  55.     BITS_SET(PORTB, PB2);      
  56.    
  57.     runstate  = 0;
  58.     signal    = 0;    // signal type
  59.     frequency = 1000; //frequency value
  60.     acc = frequency / RESOLUTION ;
  61.  
  62.  
  63.     while(1)
  64.     {
  65.         while(runstate == 1)
  66.         {
  67.             signalOutOLD(SIGNALS[signal], (uint8_t)(acc >> 16), (uint8_t)(acc >> 8), (uint8_t)acc);
  68.         }
  69.         _delay_ms(100);
  70.         BITS_FLIP(PORTB, PB1);
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement