[Atmel] 跑一下XMEGA-B1程序(2)点灯

[复制链接]
 楼主| ddllxxrr 发表于 2015-12-20 08:09 | 显示全部楼层 |阅读模式
今天这个程序很简单

首先,新建一个ASF工程,然后选择XMEGA-B1开发板,则该板上的LED及SPI还有USART都已经定义好了。

如下程序:


  1. #ifndef _XMEGA_B1_XPLAINED_H_
  2. #define _XMEGA_B1_XPLAINED_H_

  3. #include "compiler.h"
  4. #include "conf_board.h"
  5. #include "gpio.h"


  6. /*! \name Miscellaneous data
  7. */
  8. //! @{

  9. //! Validate board support for the common sensor service.

  10. #define COMMON_SENSOR_PLATFORM

  11. /*! \name Voltages
  12. */
  13. //! @{
  14. #define        VCC_TARGET      3.30
  15. #define        VCC_ANA         3.30
  16. #define        VCC_USB         5.00
  17. //! @}

  18. /*! \name ADC Connection of the Temperature Sensor
  19. */
  20. //! @{
  21. #define TEMPERATURE_SENSOR_ADC_MODULE    ADCB
  22. #define TEMPERATURE_SENSOR_ADC_INPUT     ADCCH_POS_PIN0
  23. #define TEMPERATURE_SENSOR_SIGNAL_PIN    IOPORT_CREATE_PIN(PORTB,0)
  24. //! @}

  25. /*! \name ADC Connection of the Light Sensor
  26. */
  27. //! @{
  28. #define LIGHT_SENSOR_ADC_MODULE         ADCB
  29. #define LIGHT_SENSOR_ADC_INPUT          ADCCH_POS_PIN2
  30. #define LIGHT_SENSOR_SIGNAL_PIN         IOPORT_CREATE_PIN(PORTB,2)
  31. //! @}

  32. /*! \name ADC Connection of the Potentiometer
  33. */
  34. //! @{
  35. #define POTENTIOMETER_ADC_MODULE         ADCB
  36. #define POTENTIOMETER_ADC_INPUT          ADCCH_POS_PIN1
  37. #define POTENTIOMETER_SIGNAL_PIN         IOPORT_CREATE_PIN(PORTB,1)
  38. //! @}

  39. /*! \name ADC Connection of the External Voltage Input
  40. */
  41. //! @{
  42. #define EXT_VIN_ADC_MODULE         ADCB
  43. #define EXT_VIN_ADC_INPUT          ADCCH_POS_PIN3
  44. #define EXT_VIN_SIGNAL_PIN         IOPORT_CREATE_PIN(PORTB,3)
  45. //! @}

  46. /*! \name Common ADC module used for on-board sensor.
  47. */
  48. //! @{
  49. #define BOARD_SENSOR_ADC_MODULE         ADCB
  50. //! @}

  51. /*! \name USER LEDs
  52. */
  53. //! @{
  54. #define        USERLEDPORT     PORTB
  55. #define        USERLEDMASK     (0xF0)
  56. #define        USERLED0POS     PIN4_bp

  57. static inline void user_led_set(uint8_t b)
  58. {
  59.         USERLEDPORT.OUT = b << USERLED0POS;
  60. }

  61. static inline void user_led_toggle(void)
  62. {
  63.         USERLEDPORT.OUTTGL = USERLEDMASK;
  64. }
  65. //! @}


  66. /*! \name Touch
  67. */
  68. //! @{
  69. #define        TOUCHPORT       PORTE
  70. #define        TOUCHMASK       (0x0F)
  71. #define        TOUCH0POS       PIN0_bp


  72. static inline bool is_touch0(void)
  73. {
  74.         if (TOUCHPORT.IN & (1<<(TOUCH0POS  ))) return false;
  75.         else return true;
  76. }

  77. static inline bool is_touch1(void)
  78. {
  79.         if (TOUCHPORT.IN & (1<<(TOUCH0POS+1))) return false;
  80.         else return true;
  81. }

  82. /* If USARTE0 is enabled, touch buttons CS2 and CS3 will not work, as they
  83. * share pins with the USART. These functions are then not defined if the user
  84. * tries to use them, generating an error rather than
  85. * the buttons not working. */
  86. #ifndef CONF_BOARD_ENABLE_USARTE0
  87. static inline bool is_touch2(void)
  88. {
  89.         if (TOUCHPORT.IN & (1<<(TOUCH0POS+2))) return false;
  90.         else return true;
  91. }

  92. static inline bool is_touch3(void)
  93. {
  94.         if (TOUCHPORT.IN & (1<<(TOUCH0POS+3))) return false;
  95.         else return true;
  96. }
  97. #endif

  98. static inline uint8_t touch_get(void)
  99. {
  100.         return (((~TOUCHPORT.IN) & TOUCHMASK) << TOUCH0POS);
  101. }
  102. //! @}


  103. /*! \name LCD BACKLIGHT
  104. */
  105. //! @{
  106. #define        BACKLIGHTPORT        PORTE
  107. #define BACKLIGHT_GPIO        IOPORT_CREATE_PIN(PORTE,5)
  108. #define BACKLIGHT_TIMER TCE0

  109. #ifndef BACKLIGHT_TIMER_FREQUENCY
  110. #define BACKLIGHT_TIMER_FREQUENCY        2000000
  111. #endif

  112. #ifndef        BACKLIGHT_TIMER_COUNT
  113. #define BACKLIGHT_TIMER_COUNT                0x7FFF
  114. #endif

  115. static inline void backlight_on(void)
  116. {
  117.         gpio_set_pin_high(BACKLIGHT_GPIO);
  118. }

  119. static inline void backlight_off(void)
  120. {
  121.         gpio_set_pin_low(BACKLIGHT_GPIO);
  122. }

  123. static inline void backlight_toggle(void)
  124. {
  125.         gpio_toggle_pin(BACKLIGHT_GPIO);
  126. }


  127. //! @}

  128. /*! \name Number of LEDs.
  129. */
  130. //! @{
  131. #define LED_COUNT   4
  132. //! @}

  133. /*! \name GPIO Connections of LEDs
  134. */
  135. //! @{
  136. #define LED0_GPIO   IOPORT_CREATE_PIN(PORTB,4)
  137. #define LED1_GPIO   IOPORT_CREATE_PIN(PORTB,5)
  138. #define LED2_GPIO   IOPORT_CREATE_PIN(PORTB,6)
  139. #define LED3_GPIO   IOPORT_CREATE_PIN(PORTB,7)
  140. #define LED_POWER   IOPORT_CREATE_PIN(PORTE,4)
  141. #define LED0        LED0_GPIO
  142. #define LED1        LED1_GPIO
  143. #define LED2        LED2_GPIO
  144. #define LED3        LED3_GPIO
  145. //! @}


  146. /*! \name GPIO Connections of Push Buttons
  147. * Warning, PB4 to PB7 are available only if JTAG is disabled.
  148. */
  149. //! @{
  150. #define GPIO_PUSH_BUTTON_0        IOPORT_CREATE_PIN(PORTE,0)
  151. #define GPIO_PUSH_BUTTON_1        IOPORT_CREATE_PIN(PORTE,1)
  152. #define GPIO_PUSH_BUTTON_2        IOPORT_CREATE_PIN(PORTE,2)
  153. #define GPIO_PUSH_BUTTON_3        IOPORT_CREATE_PIN(PORTE,3)
  154. //! @}

  155. /*! \name SPI Connections of the AT45DBX Data Flash Memory
  156. */
  157. //! @{
  158. #define AT45DBX_SPI           &USARTC0
  159. #define AT45DBX_CS            IOPORT_CREATE_PIN(PORTD,2)
  160. #define AT45DBX_MASTER_SCK    IOPORT_CREATE_PIN(PORTC,5)  // SCK as output
  161. #define AT45DBX_MASTER_MOSI   IOPORT_CREATE_PIN(PORTC,7)  // MOSI as output
  162. #define AT45DBX_MASTER_MISO   IOPORT_CREATE_PIN(PORTC,6)  // MISO as input
  163. #define AT45DBX_SPI_CLK_MASK  SYSCLK_PORT_C
  164. //! @}

  165. //! \name MXT143E Xplained top module
  166. //@{
  167. #define MXT143E_XPLAINED_TWI           &TWIC
  168. #define MXT143E_XPLAINED_USART_SPI     &USARTC0
  169. #define MXT143E_XPLAINED_CS            IOPORT_CREATE_PIN(PORTC, 4)
  170. #define MXT143E_XPLAINED_SCK           IOPORT_CREATE_PIN(PORTC, 7)
  171. #define MXT143E_XPLAINED_MOSI          IOPORT_CREATE_PIN(PORTC, 5)
  172. #define MXT143E_XPLAINED_MISO          IOPORT_CREATE_PIN(PORTC, 6)
  173. #define MXT143E_XPLAINED_CHG           IOPORT_CREATE_PIN(PORTC, 2)
  174. #define MXT143E_XPLAINED_DC            IOPORT_CREATE_PIN(PORTC, 3)
  175. #define MXT143E_XPLAINED_BACKLIGHT     IOPORT_CREATE_PIN(PORTA, 4)
  176. #define MXT143E_XPLAINED_LCD_RESET     IOPORT_CREATE_PIN(PORTA, 6)
  177. //@}

  178. /*! \name Power ON LED management
  179. *
  180. */
  181. //! @{
  182. static inline void led_power_on(void)
  183. {
  184.         gpio_set_pin_high(LED_POWER);
  185. }

  186. static inline void led_power_off(void)
  187. {
  188.         gpio_set_pin_low(LED_POWER);
  189. }

  190. /*! \name External oscillator
  191. */
  192. //@{
  193. #define BOARD_XOSC_HZ          8000000
  194. #define BOARD_XOSC_TYPE        XOSC_TYPE_XTAL
  195. #define BOARD_XOSC_STARTUP_US  2000
  196. //@}

  197. #ifdef CONF_BOARD_LCD_BACKLIGHT_PWM
  198.         void backlight_set_pwm(uint16_t pwm);
  199.         uint16_t backlight_get_pwm(void);
  200.         void backlight_stop_pwm(void);
  201.         void backlight_start_pwm(void);
  202. #endif

  203. #endif  // _XMEGA_B1_XPLAINED_H_
上边的:

#define LED0_GPIO   IOPORT_CREATE_PIN(PORTB,4)
#define LED1_GPIO   IOPORT_CREATE_PIN(PORTB,5)
#define LED2_GPIO   IOPORT_CREATE_PIN(PORTB,6)
#define LED3_GPIO   IOPORT_CREATE_PIN(PORTB,7)
#define LED_POWER   IOPORT_CREATE_PIN(PORTE,4)
#define LED0        LED0_GPIO
#define LED1        LED1_GPIO
#define LED2        LED2_GPIO
#define LED3        LED3_GPIO


则可以看出,用LED0,也可以写LED0_GPIO


看了一下硬件:

这四个管脚都接在了端口B上。

以下是程序:里边的换哪个写法效果都一样地

  1. #include <asf.h>

  2. int main (void)
  3. {
  4.         /* Insert system clock initialization code here (sysclk_init()). */

  5.         //sysclk_init();
  6.        
  7.         board_init();
  8.        
  9.      //ioport_set_pin_high(LED0);
  10.      //ioport_set_pin_high(LED1);
  11.          //ioport_set_pin_high(LED2);
  12.          //ioport_set_pin_high(LED3);
  13.          ioport_set_pin_high(LED0_GPIO);
  14.          ioport_set_pin_high(LED1_GPIO);
  15.          ioport_set_pin_high(LED2_GPIO);
  16.          ioport_set_pin_high(LED3_GPIO);
  17.          
  18.          
  19.          while(1);       
  20.        

  21.         /* Insert application code here, after the board has been initialized. */
  22. }
以下运行效果:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部