Advertisement
prison_lox

Lab 2 - Task 2.1 + 7 Segment

Oct 1st, 2023 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 3.87 KB | None | 0 0
  1. #include<p18F4550.inc>
  2.  
  3. CONFIG FOSC = HS ; High-Speed oscillator, HS used by USB
  4. CONFIG WDT = OFF ; Watchdog Timer disabled
  5. CONFIG DEBUG = OFF ; Debugger disabled
  6. CONFIG MCLRE = OFF ; Master Clear Reset enabled
  7. CONFIG CPUDIV = OSC1_PLL2 ; CPU system clock divisor
  8. CONFIG PBADEN = OFF ; PORTB A/D enable/disable (Disabled)
  9.  
  10. counter_value equ H'04' ; Define constant for counter value
  11.  
  12. RES_VECTOR CODE 0x0000 ; Reset vector at address 0x0000
  13. GOTO init ; Jump to the initialization code
  14.  
  15. org 0x0008 ; Interrupt vector at address 0x0008
  16. goto irq_handle ; Go to the interrupt handling function
  17.  
  18. TMR0_interrupt
  19.     ; Clear the TMR0 interrupt flag
  20.     bcf INTCON , TMR0IF ; Clear the flag
  21.     ; Increment PORTC
  22.     incf counter_value ; Increment counter value
  23.     retfie ; Return from interrupt
  24.    
  25. irq_handle ; Interrupt routine
  26.     ; Check if it is TMR0 interrupt
  27.     btfsc INTCON , TMR0IF
  28.     goto TMR0_interrupt ; Yes, it is TMR0 interrupt, jump to TMR0_interrupt
  29.     retfie ; No, return from interrupt
  30.  
  31. init
  32.     clrf TRISD ; Set PORTD as output
  33.     clrf PORTD ; Clear PORTD
  34.    
  35.     clrf TRISC ; Set PORTC as output
  36.     clrf PORTC ; Clear PORTC
  37.    
  38.     bsf T0CON , TMR0ON ; Turn ON TIMER 0
  39.     bcf T0CON , T08BIT ; Set TIMER 0 to 16 bits mode
  40.     bcf T0CON , T0CS ; Use internal clock source
  41.     bcf T0CON , PSA ; Assign prescaler to TIMER 0
  42.    
  43.     ; Setup the prescaler
  44.     bsf T0CON, T0PS2 ; Prescaler value 1:256
  45.     bcf T0CON, T0PS1 ; Prescaler value 1:256
  46.     bcf T0CON, T0PS0 ; Prescaler value 1:256
  47.    
  48.     bsf INTCON , GIE ; Enable interrupts
  49.     bsf INTCON , TMR0IE ; Enable TMR0 interrupt
  50.     clrf TMR0 ; Clear the timer
  51.    
  52.     bsf T1CON , TMR1ON ; Enable TIMER 1
  53.     bcf T1CON , TMR1CS ; Use internal clock source
  54.     clrf TMR1L ; Clear TIMER 1 low byte
  55.     clrf TMR1H ; Clear TIMER 1 high byte
  56.     GOTO main_loop ; Jump to the main loop
  57.  
  58. main_loop
  59.     call seg_set_0 ; Call segment set function for counter_value = 0
  60.     goto main_loop ; Loop indefinitely
  61.    
  62. seg_set_0
  63.     movlw H'00' ; Load value 0 into WREG
  64.     CPFSEQ counter_value ; Compare WREG with counter_value
  65.     goto seg_set_1 ; If equal, jump to seg_set_1
  66.     movlw H'3F' ; Load value 0x3F into WREG (7-segment display pattern for 0)
  67.     movwf PORTD ; Move WREG to PORTD
  68.     bsf PORTC, RC0 ; Set RC0 high
  69.     return ; Return from function
  70.    
  71. seg_set_1
  72.     movlw H'01' ; Load value 1 into WREG
  73.     CPFSEQ counter_value ; Compare WREG with counter_value
  74.     goto seg_set_2 ; If equal, jump to seg_set_2
  75.     movlw H'06' ; Load value 0x06 into WREG (7-segment display pattern for 1)
  76.     movwf PORTD ; Move WREG to PORTD
  77.     bcf PORTC, RC0 ; Set RC0 low
  78.     return ; Return from function
  79.    
  80. ; (Similar segments set functions for 2 to 9, with different display patterns)
  81. seg_set_2
  82.     movlw H'02'
  83.     CPFSEQ counter_value
  84.     goto seg_set_3
  85.     movlw H'5B'
  86.     movwf PORTD
  87.     return
  88.    
  89. seg_set_3
  90.     movlw H'03'
  91.     CPFSEQ counter_value
  92.     goto seg_set_4
  93.     movlw H'4F'
  94.     movwf PORTD
  95.     return
  96.    
  97. seg_set_4
  98.     movlw H'04'
  99.     CPFSEQ counter_value
  100.     goto seg_set_5
  101.     movlw H'66'
  102.     movwf PORTD
  103.     return
  104.    
  105. seg_set_5
  106.     movlw H'05'
  107.     CPFSEQ counter_value
  108.     goto seg_set_6
  109.     movlw H'6D'
  110.     movwf PORTD
  111.     return
  112.    
  113. seg_set_6
  114.     movlw H'06'
  115.     CPFSEQ counter_value
  116.     goto seg_set_7
  117.     movlw H'7D'
  118.     movwf PORTD
  119.     return
  120.    
  121. seg_set_7
  122.     movlw H'07'
  123.     CPFSEQ counter_value
  124.     goto seg_set_8
  125.     movlw H'07'
  126.     movwf PORTD
  127.     return
  128.    
  129. seg_set_8
  130.     movlw H'08'
  131.     CPFSEQ counter_value
  132.     goto seg_set_9
  133.     movlw H'7F'
  134.     movwf PORTD
  135.     return
  136.    
  137. seg_set_9
  138.     movlw H'09'
  139.     CPFSEQ counter_value
  140.     goto counter_reset
  141.     movlw H'6F'
  142.     movwf PORTD
  143.     return
  144.  
  145. counter_reset
  146.     clrf counter_value ; Clear counter_value
  147.     return ; Return from function
  148.  
  149. END ; End of the code  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement