Advertisement
Guest User

Untitled

a guest
Feb 20th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. #define F_CPU 128000UL
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6.  
  7.  
  8. #define MOSIPIN PB5
  9. #define REGPIN PB4
  10. #define CLK_PIN PB7
  11.  
  12. volatile int sekunde = 0;
  13. volatile int intteruptCount = 0;
  14. volatile int i=0;
  15.  
  16. void startUP() {
  17.     cli();  //Disable Interrupts
  18.  
  19.     TIMSK   |= (1<<OCIE0A) | (1<<TOIE0);        //Enable OCIE0A: Timer/Counter0 Output Compare Match A Interrupt Enable
  20.  
  21.     DDRB    = 0xFF;
  22.     PORTB   = 0x00;
  23.    
  24.     DDRD    = 0xFF;
  25.     PORTD   = 0x00;
  26.  
  27.     DDRB |= (1<<PB5);
  28.     DDRB &= ~(1<<PB6);
  29.    
  30.  
  31.  
  32.     CLKPR = 0x80;   //Ukljuci prescale originalnog takta
  33.  
  34.  
  35.     TCCR0A  |= (1 << WGM01);                // Configure Timer 0 for CTC mode
  36.     TCCR0B  |= (1 << CS02) | (1 << CS00);   //Prescaling the timer (1024)
  37.    
  38.     OCR0A   = 0xF9;                         // Set CTC compare value to 1Hz AVR clock, with pre-scaler of 1024
  39.     TCNT0   = 0x06;                         //Podesi brojac odakle da pocne da broji
  40.  
  41.  
  42.  
  43.     sei();  //Enable Interrupts
  44. }
  45.  
  46.  
  47. unsigned char SPI_SR(unsigned char data)
  48. {
  49.     USIDR = data;
  50.     USISR = (1<<USIOIF); // clear IRQ
  51.     while ((USISR & (1<<USIOIF)) == 0)// send 8 bits
  52.     {
  53.         // Positive edge SCK clock strobe
  54.         USICR = (1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC);
  55.     }
  56.     return USIDR;
  57. }
  58.  
  59. void RegisterCLK() {
  60.  
  61.     PORTB &= ~(1<<REGPIN);
  62.     _delay_ms(10);
  63.     PORTB |= (1<<REGPIN);
  64.     _delay_ms(10);
  65.     PORTB &= ~(1<<REGPIN);
  66.     _delay_ms(10);
  67. }
  68.  
  69.  
  70. //Timer0 Interrupt funkcija
  71. ISR(TIMER0_COMPA_vect)
  72. {
  73.     //PORTD ^= 0xFF;
  74.     //PORTB ^= 0xFF;
  75. }
  76.  
  77. int main(void)
  78. {
  79.  
  80.     startUP();
  81.     RegisterCLK();
  82.    
  83.  
  84.     while(1)
  85.     {
  86.         PORTB &= ~(1<<MOSIPIN);
  87.         _delay_ms(10);
  88.         for(i=0; i<8; i++) {
  89.             PORTB &= ~(1<<CLK_PIN);
  90.             _delay_ms(10);
  91.             PORTB |= (1<<CLK_PIN);
  92.             _delay_ms(10);
  93.         }
  94.         RegisterCLK();
  95.        
  96.         _delay_ms(1000);
  97.        
  98.         PORTB |= (1<<MOSIPIN);
  99.         _delay_ms(10);
  100.         for(i=0; i<8; i++) {
  101.             PORTB &= ~(1<<CLK_PIN);
  102.             _delay_ms(10);
  103.             PORTB |= (1<<CLK_PIN);
  104.             _delay_ms(10);
  105.         }
  106.         RegisterCLK();
  107.         _delay_ms(1000);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement