Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. /*
  2. * DACProject.c
  3. *
  4. * Created: 10/20/2016 9:59:41 AM
  5. * Author : Hector Farias & Jose Flores
  6. */
  7.  
  8. #define F_CPU 16000000 //define internal CLK speed
  9. // assumes vdd = 3.3v, your voltages may vary for 5v
  10. #define MOSI 3 // PB pin 3
  11. #define SCK 5 // PB pin 5
  12. #define SS 2 // PB pin 2
  13.  
  14. #include <avr/io.h>
  15. #include <util/delay.h>
  16.  
  17. void Initialize_SPI_Master(void);
  18. void Transmit_SPI_Master(int Data);
  19.  
  20. int main(void)
  21. {
  22. int count = 0;
  23. DDRB = 1<<MOSI | 1<<SCK | 1<<SS; // make MOSI, SCK and SS outputs
  24. Initialize_SPI_Master();
  25. while(1)
  26. {
  27. /**
  28. Transmit_SPI_Master(0x666); //0xAAA / 0xFFF = 2730 / 4096 * 5V (ref) = ~ 3.33V
  29. _delay_ms(100);
  30.  
  31. Transmit_SPI_Master(0x333); //0x333 / 0xFFF = 819 / 4096 * 5V (ref) ~ 0.99
  32. _delay_ms(100);
  33. **/
  34. int start = 819;
  35. int top = 1638;
  36. int step_value = 8;
  37. while(start <= top)
  38. {
  39. Transmit_SPI_Master(start);
  40. start = start + step_value;
  41.  
  42. }
  43.  
  44. start = 819;
  45.  
  46. while(top >= start)
  47. {
  48. Transmit_SPI_Master(top);
  49. top -= step_value;
  50. }
  51.  
  52. } // end while
  53. return 0;
  54. } // end main
  55.  
  56. void Initialize_SPI_Master(void)
  57. {
  58. SPCR = (0<<SPIE) | //No interrupts
  59. (1<<SPE) | //SPI enabled
  60. (1<<MSTR) ; //master
  61. SPSR = 0x00; //clear flags
  62. PORTB = 1 << SS; // make sure SS is high
  63.  
  64. }
  65.  
  66. void Transmit_SPI_Master(int Data) {
  67. PORTB &= ~(1 << SS); //Assert slave select (active low)
  68. SPDR = ((Data >> 8) & 0xF) | 0x70; //Attach configuration Bits onto MSB
  69. while (!(SPSR & (1<<SPIF)));
  70. SPDR = 0xFF & Data;
  71. while (!(SPSR & (1<<SPIF)));
  72. PORTB |= 1 << SS; //Turn off slave select
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement