Advertisement
cooprocks123e

ELEC 371 2017 final Q7

Dec 14th, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <nios2_control.h>
  3.  
  4. #define TIMER ((volatile uint32_t *)0x5000)
  5. #define INPUT ((volatile uint32_t *)0x6a00)
  6. #define OUTPUT ((volatile uint32_t *)0x7e00)
  7. #define JTAG_UART ((volatile uint32_t *)0xa800)
  8.  
  9. #define TIMER_INT (1 << 0)
  10.  
  11. #define F_CPU (50000000)
  12. #define TIMER_CYCLES ((int)(0.5 * F_CPU))
  13.  
  14. void Init(void) {
  15.     OUTPUT[0] = INPUT[0] & 0xff00 | 0x0f;
  16.  
  17.     TIMER[0] = 0; /* clear extraneous interrupt */
  18.     TIMER[2] = TIMER_CYCLES & 0xffff;
  19.     TIMER[3] = TIMER_CYCLES >> 16;
  20.     TIMER[1] = 0b0111;
  21.  
  22.     NIOS2_WRITE_IENABLE(TIMER_INT);
  23.     NIOS2_WRITE_STATUS(1);
  24. }
  25.  
  26. void PrintChar(unsigned int ch) {
  27.     while((JTAG_UART[1] & 0xffff0000) == 0) {}
  28.     JTAG_UART[0] = ch & 0xff;
  29. }
  30.  
  31. void PrintHexDigit(unsigned int n) {
  32.     unsigned int use;
  33.     if (n < 10)
  34.         use = n + '0';
  35.     else
  36.         use = n - 10 + 'A';
  37.     PrintChar(use);
  38. }
  39.  
  40. void UpdateLEDs(void) {
  41.     OUTPUT[0] ^= 0xff;
  42. }
  43.  
  44. int timercount = 0, timerflag = 0;
  45.  
  46. void interrupt_handler(void) {
  47.     uint32_t ipending = NIOS2_READ_IPENDING();
  48.     if(ipending & TIMER_INT) {
  49.         UpdateLEDs();
  50.         if(++timercount >= 10 * 2) {
  51.             timercount = 0;
  52.             timerflag = 1;
  53.         }
  54.     }
  55. }
  56.  
  57. int main(void) {
  58.     Init();
  59.  
  60.     while(1) {
  61.         if(INPUT[0] & 0x80) {
  62.             uint32_t data = INPUT[0] & 0x7f;
  63.             while(INPUT[0] & 0x80) {}
  64.             data >>= 3;
  65.             PrintHexDigit(data);
  66.             PrintChar(0x0d);
  67.         }
  68.         if(timerflag) {
  69.             timerflag = 0;
  70.             OUTPUT[0] = INPUT[0] & 0xff00 | OUTPUT[0] & 0xff;
  71.         }
  72.     }
  73.     /*
  74.         some compilers will get unhappy
  75.         if there is no return
  76.     */
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement