Advertisement
utroz

msp430_unnamed

Oct 21st, 2011
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.78 KB | None | 0 0
  1. /* Developed by Utroz & Kaddoush
  2.  * Access: http://www.oakcoders.com
  3.  * File: latch.h
  4.  *
  5.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  6.  */
  7.  
  8. /* Led Port (on/off) */
  9. #define     LED_P0      P1OUT_bit.P0    /* Led Red */
  10. #define     LED_P1      P1OUT_bit.P1    /* Led Green */
  11.  
  12. /* Default values */
  13. #define     NAND        '&'
  14. #define     NOR         '|'
  15.  
  16. /* NAND Port */
  17. #define     NAND_SET    ~(args->set & 0x0)
  18. #define     NAND_RESET  ~(args->reset & 0x0)
  19.  
  20. /* NOR Port */
  21. #define     NOR_SET     ~(args->set | 0x1)
  22. #define     NOR_RESET   ~(args->reset | 0x1)
  23.  
  24. typedef unsigned char   led;
  25. typedef unsigned char   dir;
  26. typedef signed char     latch_input;
  27. typedef signed char     latch_output;
  28.  
  29. typedef struct logic device;
  30.  
  31. struct logic
  32. {
  33.     char        logic_port;     /* Logic Name */
  34.     latch_input     set, reset;     /* Latch Input */
  35.     latch_output    q, q_;          /* Latch Output */      
  36. };
  37.  
  38. //------------------------------------
  39. /* Developed by Utroz & Kaddoush
  40.  * Access: http://www.oakcoders.com
  41.  * File: functions.h
  42.  *
  43.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  44.  */
  45.  
  46. #include <io430.h>
  47. #include <stdlib.h>
  48. #include "latch.h"
  49.  
  50. /* [ '&' to NAND ] / [ '|' to NOR ] */
  51. #define     __CIRCUIT       NOR
  52.  
  53. /* Define what LED's do you working. ( OFF: 0 / Red: 1 / Green: 2 / Both: 3) */
  54. #define     __TOGGLE_LED    3
  55.  
  56.     void    call(void);
  57.     void    delay(void);
  58.         void    push_button_modules(void);
  59.     dir     get_toggle_led(void);
  60. static  led     __set_latch(device *args);
  61. static  led     __reset_latch(device *args);
  62. static  void    __start_port(device *args);
  63.  
  64. //------------------------------------
  65. /* Developed by Utroz & Kaddoush
  66.  * Access: http://www.oakcoders.com
  67.  * File: functions.c
  68.  *
  69.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  70.  */
  71.  
  72. #include "functions.h"
  73.  
  74. void call(void)
  75. {
  76.         device *port = malloc(sizeof(device));
  77.    
  78.         if (port == NULL) {
  79.             /* Memory could not be allocated */
  80.         free(port); /* Allocated Memory is Free */    
  81.        
  82.         } else { /* Allocation succeeded.  Do something. */    
  83.             __start_port(port); /* The pointed-to-data  must not be used again. */
  84.         }  
  85.  
  86.     #if (__TOGGLE_LED == 1) // Red.
  87.             if (LED_P0 == 0x1)
  88.                 LED_P0 = __reset_latch(port); //P1OUT_bit.P0 = 0x00;
  89.             else
  90.                 LED_P0 = __set_latch(port); //P1OUT_bit.P0 = 0x01; 
  91.        
  92.     #elif (__TOGGLE_LED == 2) // Green.
  93.         if (LED_P1 == 0x1)
  94.                 LED_P1 = __reset_latch(port); //P1OUT_bit.P0 = 0x00;
  95.             else
  96.                 LED_P1 = __set_latch(port); //P1OUT_bit.P0 = 0x01;
  97.        
  98.     #else // Both LED's
  99.         if (LED_P0 == 0x1 & LED_P1 == 0x1){
  100.                 LED_P0 = __reset_latch(port); //P1OUT_bit.P0 = 0x01;   
  101.             LED_P1 = __reset_latch(port); //P1OUT_bit.P1 = 0x01;   
  102.         } else {
  103.                 LED_P0 = __set_latch(port); //P1OUT_bit.P0 = 0x01;  
  104.             LED_P1 = __set_latch(port); //P1OUT_bit.P1 = 0x01;  
  105.         }
  106.     #endif
  107.        
  108.     free(port); /* Allocated memory is Free */
  109. }
  110.  
  111. inline void delay(void)  
  112. {
  113.         asm("  mov    #0Ah,  R14       ");  // Load loop counter  
  114.         asm("  mov    #0FFh,  R15      ");  // Load loop counter  
  115.         asm("  dec    R15              ");  // Decrements the R15 register.  
  116.         asm("  jnz    $-2              ");  // Back 2 positions on the memory.
  117.         asm("  dec    R14              ");  // Decrements the R14 register.  
  118.         asm("  jnz    $-4              ");  // Back 4 positions on the memory.  
  119. }
  120.  
  121. void push_button_modules(void)
  122. {
  123.         /* Put P2 port how input (Push Button) */
  124.         P1DIR_bit.P2 = 0x0;
  125.    
  126.     /* Enable the internal resistor of
  127.         * the button */
  128.         P1REN |= 0x04;
  129.    
  130.         /* Enable interruption on P1.2 */
  131.         P1IE |= 0x04;
  132.    
  133.     __bis_SR_register(GIE); /* Interrupção Global */
  134. }
  135.  
  136. /* This Return Dir based on the Define __TOGGLE_LED*/
  137. dir get_toggle_led(void)
  138. {
  139.     /* Set LED Dir (input/output) */
  140.     if (__TOGGLE_LED == 3)
  141.         return 0x03;
  142.     else if (__TOGGLE_LED == 1 || __TOGGLE_LED == 2)
  143.             return (__TOGGLE_LED == 1) ? 0x01 : 0x02; /* Toggle only one LED (RED/GREEN */
  144.        
  145.     else
  146.         return NULL; /* Turn's Power OFF */
  147. }
  148.  
  149. /* This function was created, which objective is set a LATCH. */
  150. static led __set_latch(device *args)
  151. {
  152.         /* Set LATCH with the NAND logic circuit in the bitwise mode */
  153.         #if (__CIRCUIT == NAND)
  154.             args->set = 0x0;
  155.             args->reset = ~(args->set);
  156.      
  157.             args->q = NAND_SET;  
  158.         args->q_ = ~(args->q);
  159.        
  160.         return (args->q); // latch out
  161.  
  162.         /* Set LATCH with the NOR logic circuit in the bitwise mode */
  163.         #else
  164.         args->set = 0x0;
  165.             args->reset = ~(args->set);
  166.        
  167.             args->q_ = NOR_SET;  
  168.         args->q = ~(args->q_);
  169.        
  170.             return (args->q); // latch out
  171.         #endif
  172. }
  173.  
  174. /* This function was created, which objective is set the LATCH. */
  175. static led __reset_latch(device *args)
  176. {
  177.         /* Reset LATCH with the NAND logic circuit in the bitwise mode */
  178.         #if (__CIRCUIT == NAND)
  179.             args->reset = 0x0;
  180.             args->set = ~(args->reset);
  181.        
  182.         args->q_ = NAND_RESET;  
  183.         args->q = ~(args->q_);
  184.  
  185.             return args->q; // latch out
  186.  
  187.         /* Reset LATCH with NOR logic circuit in the bitwise mode */
  188.         #else
  189.         args->reset = 0x0;
  190.             args->set = ~(args->reset);
  191.        
  192.         args->q = NOR_RESET;  
  193.         args->q_ = ~(args->q);
  194.        
  195.             return args->q; // latch out
  196.         #endif
  197. }
  198.  
  199. static void __start_port(device *args)
  200. {
  201.         /* Start up initial values of the NAND port.*/
  202.         #if (__CIRCUIT == NAND)
  203.             args->logic_port = NAND;
  204.        
  205.         /* Latch Input */
  206.             args->set = 0x1;
  207.             args->reset = 0x1;
  208.        
  209.         /* Latch Output */
  210.         args->q = 0x00;
  211.         args->q_ = ~args->q;
  212.        
  213.         /* Start up initial values of the NOR port.*/        
  214.         #elif (__CIRCUIT == NOR)
  215.             args->logic_port = NOR;
  216.        
  217.         /* Latch Input */      
  218.             args->set = 0x0;
  219.             args->reset = 0x0;
  220.        
  221.         /* Latch Output */
  222.         args->q = 0x00;
  223.         args->q_ = ~args->q;
  224.        
  225.         #else
  226.             #error (ERROR 0x00): "Please, put a correct logic circuit in the 'define' __CIRCUIT."
  227.         #endif
  228. }  
  229.  
  230. //------------------------------------
  231. /* Developed by Utroz & Kaddoush
  232.  * Access: http://www.oakcoders.com
  233.  * File: interrupt.h
  234.  *
  235.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  236.  */
  237.  
  238. #define CP_PORT1_VECTOR         (2 * 2u)  /* 0xFFE4 Port 1 */
  239. #define HIBERNATE       (0x00)
  240. #define POWER           ( WDT_ADLY_1_9 )
  241.  
  242. #define BOARD_ON        0x1;
  243. #define BOARD_OFF       0x0;
  244.  
  245. typedef unsigned char current;
  246.  
  247. extern  unsigned char   get_toggle_led();
  248.  
  249. #pragma vector = CP_PORT1_VECTOR
  250. __interrupt void Port_1 (void);
  251.  
  252. //------------------------------------
  253. /* Developed by Utroz & Kaddoush
  254.  * Access: http://www.oakcoders.com
  255.  * File: interrupt.c
  256.  *
  257.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  258.  */
  259.  
  260. #include "interrupt.h"
  261. #include <io430.h>
  262.  
  263. #pragma vector = CP_PORT1_VECTOR
  264. __interrupt void Port_1 (void)
  265. {
  266.     static current board_status = BOARD_ON;
  267.    
  268.     P1IFG &= ~0x04; /* Clean the register flag of interrupt */
  269.  
  270.     if(board_status) {
  271.             P1DIR &= HIBERNATE;
  272.         board_status = BOARD_OFF;
  273.     }
  274.     else {
  275.         P1DIR = get_toggle_led();
  276.         board_status = BOARD_ON;
  277.     }
  278. }
  279.  
  280. //------------------------------------
  281. /* Developed by Utroz & Kaddoush
  282.  * Access: http://www.oakcoders.com
  283.  * File: main.c
  284.  *
  285.  * printk(KERN_DEBUG "Dennis Ritchie(d.m.r) died. Rest in peace.");
  286.  */
  287.  
  288. #include "io430x22x4.h"
  289. #include "interrupt.h"
  290.  
  291. extern  void        call(void);
  292. extern  void        delay(void);
  293. extern  unsigned char   get_toggle_led();
  294. extern  void        push_button_modules(void);
  295.  
  296. int main(void)
  297. {          
  298.     WDTCTL = ( POWER ); /* Disables the watch dog timer */     
  299.     P1DIR = get_toggle_led();
  300.    
  301.     push_button_modules(); /* Initializes push button modules. */
  302.  
  303.     while(1){
  304.         call();
  305.         delay();
  306.     }      
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement