document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // hello.stepper.bipolar.44.full.c
  2. //
  3. // bipolar full stepping hello-world
  4. //
  5. // Neil Gershenfeld
  6. // 11/21/12
  7. //
  8. // modified by Luis Díaz
  9. // 05/12
  10. //
  11. //
  12. //
  13. // (c) Massachusetts Institute of Technology 2012
  14. // This work may be reproduced, modified, distributed,
  15. // performed, and displayed for any purpose. Copyright is
  16. // retained and must be preserved. The work is provided
  17. // as is; no warranty is provided, and users accept all
  18. // liability.
  19. //
  20.  
  21. /**
  22.  * \\mainpage User Application template doxygen documentation
  23.  *
  24.  * \\par Empty user application template
  25.  *
  26.  * Bare minimum empty user application template
  27.  *
  28.  * \\par Content
  29.  *
  30.  * -# Include the ASF header files (through asf.h)
  31.  * -# "Insert system clock initialization code here" comment
  32.  * -# Minimal main function that starts with a call to board_init()
  33.  * -# "Insert application code here" comment
  34.  *
  35.  */
  36.  
  37. /*
  38.  * Include header files for all drivers that have been imported from
  39.  * Atmel Software Framework (ASF).
  40.  */
  41. /*
  42.  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  43.  */
  44. #include <asf.h>
  45.  
  46. //#define MOTORTEST
  47. #define ADCDAC
  48. //#define SINETEST
  49.  
  50.  
  51. //Stepper driver pins
  52. #define A2 IOPORT_CREATE_PIN(PORTC, 1)
  53. #define A1 IOPORT_CREATE_PIN(PORTC, 2)
  54. #define B2 IOPORT_CREATE_PIN(PORTC, 3)
  55. #define B1 IOPORT_CREATE_PIN(PORTC, 4)
  56.  
  57. // ADC/DAC
  58. #define DAC1 IOPORT_CREATE_PIN(PORTA, 3)
  59. #define DAC2 IOPORT_CREATE_PIN(PORTA, 2)
  60.  
  61. #define ADC1 IOPORT_CREATE_PIN(PORTD, 4)
  62.  
  63. #define MY_ADC ADCA
  64. #define ADC_SND_CH ADC_CH0 //PD4
  65.  
  66. #define SPEAKER_DAC              DACA
  67. #define SPEAKER_DAC_CHANNEL      DAC_CH0
  68. #define RATE_OF_CONVERSION       22050
  69. #define NR_OF_SAMPLES            32
  70.  
  71. //LED pins
  72. #define GL IOPORT_CREATE_PIN(PORTR, 0) //blue led
  73. #define BL IOPORT_CREATE_PIN(PORTR, 1)  //green led
  74.  
  75. #define on_delay() delay_us(25) // PWM on time
  76. #define off_delay() delay_us(5) // PWM off time
  77. #define PWM_count 100 // number of PWM cycles
  78. #define step_count 20 // number of steps
  79.  
  80. static const uint16_t sine[NR_OF_SAMPLES] = {
  81.     32768, 35325, 37784, 40050, 42036, 43666, 44877, 45623,
  82.     45875, 45623, 44877, 43666, 42036, 40050, 37784, 35325,
  83.     32768, 30211, 27752, 25486, 23500, 21870, 20659, 19913,
  84.     19661, 19913, 20659, 21870, 23500, 25486, 27752, 30211,
  85. };
  86.  
  87. static uint8_t count;
  88.  
  89. uint8_t i = 0;
  90.  
  91. void pulse_ApBp(void);
  92. void pulse_AmBp(void);
  93. void pulse_ApBm(void);
  94. void pulse_AmBm(void);
  95. void step_ccw(void);
  96. void step_cw(void);
  97. uint16_t read_sound(void);
  98. void write_sound(uint16_t);
  99. void write_sine(void);
  100. static void adc_init(void);
  101. static void evsys_init(void);
  102. void tc_init(void);
  103. void dac_init(void);
  104.  
  105. int main (void)
  106. {
  107.     /* Insert system clock initialization code here (sysclk_init()). */
  108.     sysclk_init();
  109.    
  110.     #if defined(ADCDAC) || defined(SINETEST)
  111.         evsys_init();
  112.         tc_init();
  113.         dac_init();
  114.         adc_init();
  115.     #endif
  116.    
  117.     board_init();
  118.     delay_init();
  119.     //
  120.     // main
  121.     //
  122.     #ifdef MOTORTEST
  123.     static uint8_t i,j;
  124.     #endif
  125.     //
  126.  
  127.     //
  128.     // initialize bridge pins
  129.     //
  130.     //PC1 to PC4 as outputs
  131.     ioport_set_pin_dir(A1, IOPORT_DIR_OUTPUT);
  132.     ioport_set_pin_dir(A2, IOPORT_DIR_OUTPUT);
  133.     ioport_set_pin_dir(B1, IOPORT_DIR_OUTPUT);
  134.     ioport_set_pin_dir(B2, IOPORT_DIR_OUTPUT);
  135.    
  136.     ioport_set_pin_dir(ADC1, IOPORT_DIR_INPUT);
  137.     ioport_set_pin_dir(DAC1, IOPORT_DIR_OUTPUT);
  138.     ioport_set_pin_dir(DAC2, IOPORT_DIR_OUTPUT);
  139.  
  140.     ioport_set_pin_dir(BL, IOPORT_DIR_OUTPUT);
  141.     ioport_set_pin_dir(GL, IOPORT_DIR_OUTPUT);
  142.  
  143.     ioport_set_pin_low(A1);
  144.     ioport_set_pin_low(A2);
  145.     ioport_set_pin_low(B1);
  146.     ioport_set_pin_low(B2);
  147.  
  148.     ioport_set_pin_high(BL);
  149.     ioport_set_pin_high(GL);
  150.     delay_s(1);
  151.     ioport_set_pin_low(BL);
  152.     uint16_t adc_value = 0;
  153.     //
  154.     // main loop
  155.     //
  156.     while (1) {
  157.         #ifdef MOTORTEST
  158.         for (i = 0; i < step_count; ++i) {
  159.             for (j = 0; j < i; ++j)
  160.             step_cw();
  161.             for (j = 0; j < i; ++j)
  162.             step_ccw();
  163.         }
  164.         for (i = step_count; i > 0; --i) {
  165.             for (j = 0; j < i; ++j)
  166.             step_cw();
  167.             for (j = 0; j < i; ++j)
  168.             step_ccw();
  169.         }
  170.         #endif
  171.  
  172.         #ifdef ADCDAC
  173.            
  174.             adc_value = read_sound();
  175.            
  176.                 ioport_set_pin_high(BL);
  177.                 delay_ms(adc_value);
  178.                 ioport_set_pin_low(BL);
  179.                 delay_ms(adc_value);
  180.                
  181.             //write_sound(adc_value);
  182.         #endif
  183.  
  184.         #ifdef SINETEST
  185.             write_sine();
  186.         #endif
  187.     }
  188. }
  189.  
  190. //
  191. // A+ B+ PWM pulse
  192. //
  193. void pulse_ApBp() {
  194.     ioport_set_pin_low(A2);
  195.     ioport_set_pin_low(B2);
  196.     ioport_set_pin_high(A1);
  197.     ioport_set_pin_high(B1);
  198.     for (count = 0; count < PWM_count; ++count) {
  199.         ioport_set_pin_high(A1);
  200.         ioport_set_pin_high(B1);
  201.         on_delay();
  202.         ioport_set_pin_low(A1);
  203.         ioport_set_pin_low(B1);
  204.         off_delay();
  205.     }
  206. }
  207. //
  208. // A+ B- PWM pulse
  209. //
  210. void pulse_ApBm() {
  211.     ioport_set_pin_low(A2);
  212.     ioport_set_pin_low(B1);
  213.     ioport_set_pin_high(A1);
  214.     ioport_set_pin_high(B2);
  215.     for (count = 0; count < PWM_count; ++count) {
  216.         ioport_set_pin_high(A1);
  217.         ioport_set_pin_high(B2);
  218.         on_delay();
  219.         ioport_set_pin_low(A1);
  220.         ioport_set_pin_low(B2);
  221.         off_delay();
  222.     }
  223. }
  224. //
  225. // A- B+ PWM pulse
  226. //
  227. void pulse_AmBp() {
  228.     ioport_set_pin_low(A1);
  229.     ioport_set_pin_low(B2);
  230.     ioport_set_pin_high(A2);
  231.     ioport_set_pin_high(B1);
  232.     for (count = 0; count < PWM_count; ++count) {
  233.         ioport_set_pin_high(A2);
  234.         ioport_set_pin_high(B1);
  235.         on_delay();
  236.         ioport_set_pin_low(A2);
  237.         ioport_set_pin_low(B1);
  238.         off_delay();
  239.     }
  240. }
  241. //
  242. // A- B- PWM pulse
  243. //
  244. void pulse_AmBm() {
  245.     ioport_set_pin_low(A1);
  246.     ioport_set_pin_low(B1);
  247.     ioport_set_pin_high(A2);
  248.     ioport_set_pin_high(B2);
  249.     for (count = 0; count < PWM_count; ++count) {
  250.         ioport_set_pin_high(A2);
  251.         ioport_set_pin_high(B2);
  252.         on_delay();
  253.         ioport_set_pin_low(A2);
  254.         ioport_set_pin_low(B2);
  255.         off_delay();
  256.     }
  257. }
  258. //
  259. // clockwise step
  260. //
  261. void step_cw() {
  262.     pulse_ApBp();
  263.     pulse_AmBp();
  264.     pulse_AmBm();
  265.     pulse_ApBm();
  266. }
  267. //
  268. // counter-clockwise step
  269. //
  270. void step_ccw() {
  271.     pulse_ApBm();
  272.     pulse_AmBm();
  273.     pulse_AmBp();
  274.     pulse_ApBp();
  275. }
  276.  
  277. //
  278. // read adc
  279. //
  280.  
  281. static void adc_init(void)
  282. {
  283.     struct adc_config adc_conf;
  284.     struct adc_channel_config adcch_conf;
  285.     adc_read_configuration(&MY_ADC, &adc_conf);
  286.     adcch_read_configuration(&MY_ADC, ADC_SND_CH, &adcch_conf);
  287.     adc_set_conversion_parameters(&adc_conf, ADC_SIGN_OFF, ADC_RES_12,
  288.     ADC_REF_VCC);
  289.     adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
  290.     adc_set_clock_rate(&adc_conf, 200000UL);
  291.     adcch_set_input(&adcch_conf, ADCCH_POS_PIN12, ADCCH_NEG_NONE, 1);
  292.     adc_write_configuration(&MY_ADC, &adc_conf);
  293.     adcch_write_configuration(&MY_ADC, ADC_SND_CH, &adcch_conf);
  294. }
  295.  
  296. uint16_t read_sound()
  297. {
  298.     uint16_t result;
  299.     adc_enable(&MY_ADC);
  300.     adc_start_conversion(&MY_ADC, ADC_SND_CH);
  301.     adc_wait_for_interrupt_flag(&MY_ADC, ADC_SND_CH);
  302.     result = adc_get_result(&MY_ADC, ADC_SND_CH);
  303.     return result;
  304. }
  305.  
  306. static void evsys_init(void)
  307. {
  308.     sysclk_enable_module(SYSCLK_PORT_GEN, SYSCLK_EVSYS);
  309.     EVSYS.CH3MUX = EVSYS_CHMUX_TCC4_OVF_gc;
  310. }
  311. void tc_init(void)
  312. {
  313.     tc45_enable(&TCC4);
  314.     tc45_set_wgm(&TCC4, TC45_WG_NORMAL);
  315.     tc45_write_period(&TCC4, (sysclk_get_per_hz() / RATE_OF_CONVERSION) - 1);
  316.     tc45_write_clock_source(&TCC4, TC45_CLKSEL_DIV1_gc);
  317. }
  318. void dac_init(void)
  319. {
  320.     struct dac_config conf;
  321.     dac_read_configuration(&SPEAKER_DAC, &conf);
  322.     dac_set_conversion_parameters(&conf, DAC_REF_BANDGAP, DAC_ADJ_LEFT);
  323.     dac_set_active_channel(&conf, SPEAKER_DAC_CHANNEL, 0);
  324.     dac_set_conversion_trigger(&conf, SPEAKER_DAC_CHANNEL, 3);
  325.     #ifdef XMEGA_DAC_VERSION_1
  326.     dac_set_conversion_interval(&conf, 1);
  327.     #endif
  328.     dac_write_configuration(&SPEAKER_DAC, &conf);
  329.     dac_enable(&SPEAKER_DAC);
  330. }
  331.  
  332. void write_sound(uint16_t value)
  333. {
  334.         dac_wait_for_channel_ready(&SPEAKER_DAC, SPEAKER_DAC_CHANNEL);
  335.         dac_set_channel_value(&SPEAKER_DAC, SPEAKER_DAC_CHANNEL, value);
  336.         }
  337.  
  338. void write_sine(void)
  339. {
  340.     dac_wait_for_channel_ready(&SPEAKER_DAC, SPEAKER_DAC_CHANNEL);
  341.     dac_set_channel_value(&SPEAKER_DAC, SPEAKER_DAC_CHANNEL, sine[i]);
  342.     i++;
  343.     i %= NR_OF_SAMPLES;
  344. }
');