Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;********************************************************************
  2. ; Authors       : Tomer Gross - 322712753, Yaniv Korobkin - 207329525
  3. ; Date          : 14 on January, 2020
  4. ; File          : voltmeter_project.asm
  5. ; Description   : sample the voltage of the analog signal and send it through the uart
  6.                 ; every time that 'M' pressed
  7. ;********************************************************************
  8. #include<ADUC841.H>
  9.  
  10. BSEG  ; define flags
  11.     ADC_FLAG: DBIT 0  ; set when adc done sample
  12.     TI_FLAG: DBIT 0  ; set when TI is set
  13.     RI_FLAG: DBIT 0  ; set when we received 'M'
  14.  
  15. CSEG AT 00h
  16.     JMP START
  17.  
  18. CSEG AT 23h  ; serial ISR
  19.     JMP CHECK_INPUT
  20.  
  21. CSEG AT 33H  ; adc ISR
  22.     SETB ADC_FLAG
  23. RETI
  24.  
  25.      
  26. CSEG AT 0100H
  27. ;______________________________________________________________________________
  28.                                                     ; SERIAL INTERRUPT HANDLING
  29.                                                    
  30. ; in addition to prevent ovarlapping we handling the serial interrupt outside of the ISR                                                   
  31. CHECK_INPUT:
  32.     JBC TI, DONE_TRANSMITTING  ; if done transmiting then jump and clear TI
  33.    
  34.     CHECK:
  35.         MOV R1, SBUF  ; get the pressed key's ASCII value  
  36.         CJNE R1, #'M', NOT_M_ME  ; in the case which the pressed key isnt 'M'
  37.         SETB RI_FLAG
  38.         JMP NOT_M  ; we need to clear TI&RI anyway to prevent redundent call for the interrupt service routine
  39.    
  40.     DONE_TRANSMITTING:
  41.     SETB TI_FLAG  ; set to alert that a new character can be trasmitting
  42.    
  43.     NOT_M_ME:
  44.     JMP INVALID_INPUT
  45.     RETI
  46.    
  47.     NOT_M:
  48.     CLR RI
  49.     RETI
  50. ;____________________________________________________________________
  51.                                                     ; MAIN PROGRAM
  52. START:
  53.     ; set UART
  54.     ; puts the UART in 8-bit variable baud rate mode
  55.     CLR SM0
  56.     SETB SM1
  57.     SETB REN  ;allow recieved signals
  58.    
  59.     ; setting timer 3
  60.     ANL T3CON, #11111010B ; bit7 = enable bit, reserving bits 6-3, we allow only mode 010 (DIV=2)
  61.     ORL T3CON, #10000010B ; bit7 = enables Timer 3 to provide the clock to the UART, bits 2-0: DIV = 2 = 010
  62.     MOV T3FD, #32 ; baud-rate=115200bps according to formulas (as shown in the documentations)
  63.  
  64.     ; ADC setup
  65.     MOV ADCCON1, #10111000B ; bit7 = adc mode is on, bit6 = disable external vref (as default its 2.5[v] - as we want), reserving bits 5-3, disable 3 last bits
  66.     MOV ADCCON2, #00000000B ; we want to ensure that adc2 isnt working
  67.  
  68.     SETB ES  ; allow serial interrupts
  69.     SETB EADC  ; allow adc interrupts
  70.     SETB EA  ; allow global interrupts (should be last!)
  71.    
  72.  
  73. POLLING:
  74.     ; busy wait  - release when we receive 'M'
  75.     JNB RI_FLAG, $
  76.     CLR RI_FLAG  ; allow to get more 'M's
  77.    
  78. ;____________________________________________________________________
  79.                                                     ; SUBROUTINES
  80. SAMPLE:
  81.     PUSH PSW
  82.     PUSH ACC
  83.    
  84.     SETB SCONV  ; sample the analog signal
  85.  
  86.     ; waiting for the adc to finish sample
  87.     JNB ADC_FLAG, $
  88.     CLR ADC_FLAG
  89.  
  90.  
  91. PROGRAM:
  92.     MOV A, ADCDATAL  ; save the low order adc data into the accumulator (we will take only the 4MSBs)
  93.     SWAP A  ; we want the 4MSBs to be in the low order location
  94.     ANL A, #00001111B  ; we dont care about 4 MSB now because they were the 4LSBs before the swap
  95.     MOV R2, A  ; R2 saves the 4lsb in the new 8bit combined register
  96.    
  97.     MOV A, ADCDATAH  ; we know that in 8 bit mode the 4 high bits of ADCDATAH are zeros anyway
  98.     SWAP A  ; we want the 4LSBs to be in the high order location
  99.     MOV R3, A  ; R3 saves the 4msbs in the new 8bit combined register
  100.    
  101.     CLR A  ; ensures that accumulator is cleared
  102.    
  103.     ; combine the results into 8bit register
  104.     MOV A, R2
  105.     ADD A, R3
  106.     MOV R4, A ; combined
  107.        
  108.    
  109.     MOV A, R4  ; the accumulator saves the offset divided by 4, from the start of the volts table
  110.     MOV B, #4D  ; multiply it by 4 to get the real offset of the 4 bits string (any volt lvl is in the format: "x.xx", length of 4)
  111.     MUL AB  ; according to table we built, each voltage level its in location of multiplication of 4
  112.     MOV DPTR, #VOLTS  ; dptr point to the start of the table
  113.    
  114.    
  115.     ; max offset is 250(for 2.5 volts)*4 = 1000d that can be represented in atleast 10 bits so its devided between two 8 bits register
  116.     ; that can save together offset of length 16 bits
  117.    
  118.     ; A contains the 8 low bits of the offset - going to DPL
  119.     ADD A, DPL
  120.     MOV DPL, A
  121.    
  122.     ; B contains the 8 high bits of the offset - going to DPH
  123.     MOV A, B
  124.     ADD A, DPH
  125.     MOV DPH, A
  126.  
  127.     ;MOV R3, A ; the string address
  128.     ;MOV DPTR, A
  129.    
  130.    
  131.     MOV R5, #4D ; repeat 4 times - length of output
  132.     CLR REN  ; block received signals while transmiting string
  133. TRAN_STRING:
  134.     CLR A
  135.     MOVC A, @A+DPTR  ; get character
  136.     MOV SBUF, A  ; transmit to the uart
  137.     JNB TI_FLAG, $  ; wait for transmitting over
  138.     CLR TI_FLAG  ; allow transmitting other characters
  139.     INC DPTR  ; move on to the next character
  140.     DJNZ R5, TRAN_STRING  ; repeat r5 times
  141.    
  142.     MOV SBUF, #13D  ; transmit carriage return
  143.     JNB TI_FLAG, $
  144.     CLR TI_FLAG
  145.     MOV SBUF, #10D  ; transmit line feed
  146.     JNB TI_FLAG, $
  147.     CLR TI_FLAG
  148.    
  149.     SETB REN  ; allow signals again
  150.     POP ACC
  151.     POP PSW
  152.    
  153.     JMP POLLING
  154.    
  155.    
  156.     INVALID_INPUT:  CLR REN
  157.                 PUSH PSW
  158.                 PUSH ACC
  159.                 CLR A
  160.                 MOV DPTR, #ERR
  161.                 MOV R5, #16D
  162.      PRINTING: 
  163.                 MOVC A, @A+DPTR
  164.                 MOV SBUF, A
  165.                 JNB TI_FLAG, $  ; wait for transmitting over
  166.                 CLR TI_FLAG
  167.                 INC DPTR  ; move on to the next character
  168.                 DJNZ R5, PRINTING
  169.                 SETB REN  ; allow signals again
  170.                 POP ACC
  171.                 POP PSW
  172.                 SETB REN
  173.                
  174.                 RETI
  175. ;____________________________________________________________________  
  176.                                                         ; VOLTS TABLE
  177.  
  178. CSEG AT 150H
  179. ERR:
  180.     DB 'INVALID ERROR'
  181.     DB 13
  182.     DB 10
  183.     DB 0
  184.  
  185. CSEG AT 0200H
  186.  
  187. VOLTS:
  188.  
  189.     ; theoretically the voltage that can be represented in 8 bit register can arrive to 2.55[v] but
  190.     ; practically the Vref is just 2.5[v] so its never can reach this value
  191.     DB '0.00'
  192.     DB '0.01'
  193.     DB '0.02'
  194.     DB '0.03'
  195.     DB '0.04'
  196.     DB '0.05'
  197.     DB '0.06'
  198.     DB '0.07'
  199.     DB '0.08'
  200.     DB '0.09'
  201.     DB '0.10'
  202.     DB '0.11'
  203.     DB '0.12'
  204.     DB '0.13'
  205.     DB '0.14'
  206.     DB '0.14'
  207.     DB '0.15'
  208.     DB '0.16'
  209.     DB '0.17'
  210.     DB '0.18'
  211.     DB '0.19'
  212.     DB '0.20'
  213.     DB '0.21'
  214.     DB '0.22'
  215.     DB '0.23'
  216.     DB '0.24'
  217.     DB '0.25'
  218.     DB '0.26'
  219.     DB '0.27'
  220.     DB '0.28'
  221.     DB '0.29'
  222.     DB '0.30'
  223.     DB '0.31'
  224.     DB '0.32'
  225.     DB '0.33'
  226.     DB '0.34'
  227.     DB '0.35'
  228.     DB '0.36'
  229.     DB '0.37'
  230.     DB '0.38'
  231.     DB '0.39'
  232.     DB '0.40'
  233.     DB '0.41'
  234.     DB '0.42'
  235.     DB '0.43'
  236.     DB '0.44'
  237.     DB '0.45'
  238.     DB '0.46'
  239.     DB '0.47'
  240.     DB '0.48'
  241.     DB '0.49'
  242.     DB '0.50'
  243.     DB '0.51'
  244.     DB '0.52'
  245.     DB '0.53'
  246.     DB '0.54'
  247.     DB '0.55'
  248.     DB '0.56'
  249.     DB '0.57'
  250.     DB '0.58'
  251.     DB '0.59'
  252.     DB '0.60'
  253.     DB '0.61'
  254.     DB '0.62'
  255.     DB '0.63'
  256.     DB '0.64'
  257.     DB '0.65'
  258.     DB '0.66'
  259.     DB '0.67'
  260.     DB '0.68'
  261.     DB '0.69'
  262.     DB '0.70'
  263.     DB '0.71'
  264.     DB '0.72'
  265.     DB '0.73'
  266.     DB '0.74'
  267.     DB '0.75'
  268.     DB '0.76'
  269.     DB '0.77'
  270.     DB '0.78'
  271.     DB '0.79'
  272.     DB '0.80'
  273.     DB '0.81'
  274.     DB '0.82'
  275.     DB '0.83'
  276.     DB '0.84'
  277.     DB '0.85'
  278.     DB '0.86'
  279.     DB '0.87'
  280.     DB '0.88'
  281.     DB '0.89'
  282.     DB '0.90'
  283.     DB '0.91'
  284.     DB '0.92'
  285.     DB '0.93'
  286.     DB '0.94'
  287.     DB '0.95'
  288.     DB '0.96'
  289.     DB '0.97'
  290.     DB '0.98'
  291.     DB '0.99'
  292.     DB '1.00'
  293.     DB '1.01'
  294.     DB '1.02'
  295.     DB '1.03'
  296.     DB '1.04'
  297.     DB '1.05'
  298.     DB '1.06'
  299.     DB '1.07'
  300.     DB '1.08'
  301.     DB '1.09'
  302.     DB '1.10'
  303.     DB '1.11'
  304.     DB '1.12'
  305.     DB '1.13'
  306.     DB '1.14'
  307.     DB '1.15'
  308.     DB '1.16'
  309.     DB '1.17'
  310.     DB '1.18'
  311.     DB '1.19'
  312.     DB '1.20'
  313.     DB '1.21'
  314.     DB '1.22'
  315.     DB '1.23'
  316.     DB '1.24'
  317.     DB '1.25'
  318.     DB '1.26'
  319.     DB '1.27'
  320.     DB '1.28'
  321.     DB '1.29'
  322.     DB '1.30'
  323.     DB '1.31'
  324.     DB '1.32'
  325.     DB '1.33'
  326.     DB '1.34'
  327.     DB '1.35'
  328.     DB '1.36'
  329.     DB '1.37'
  330.     DB '1.38'
  331.     DB '1.39'
  332.     DB '1.40'
  333.     DB '1.41'
  334.     DB '1.42'
  335.     DB '1.43'
  336.     DB '1.44'
  337.     DB '1.45'
  338.     DB '1.46'
  339.     DB '1.47'
  340.     DB '1.48'
  341.     DB '1.49'
  342.     DB '1.50'
  343.     DB '1.51'
  344.     DB '1.52'
  345.     DB '1.53'
  346.     DB '1.54'
  347.     DB '1.55'
  348.     DB '1.56'
  349.     DB '1.57'
  350.     DB '1.58'
  351.     DB '1.59'
  352.     DB '1.60'
  353.     DB '1.61'
  354.     DB '1.62'
  355.     DB '1.63'
  356.     DB '1.64'
  357.     DB '1.65'
  358.     DB '1.66'
  359.     DB '1.67'
  360.     DB '1.68'
  361.     DB '1.69'
  362.     DB '1.70'
  363.     DB '1.71'
  364.     DB '1.72'
  365.     DB '1.73'
  366.     DB '1.74'
  367.     DB '1.75'
  368.     DB '1.76'
  369.     DB '1.77'
  370.     DB '1.78'
  371.     DB '1.79'
  372.     DB '1.80'
  373.     DB '1.81'
  374.     DB '1.82'
  375.     DB '1.83'
  376.     DB '1.84'
  377.     DB '1.85'
  378.     DB '1.86'
  379.     DB '1.87'
  380.     DB '1.88'
  381.     DB '1.89'
  382.     DB '1.90'
  383.     DB '1.91'
  384.     DB '1.92'
  385.     DB '1.93'
  386.     DB '1.94'
  387.     DB '1.95'
  388.     DB '1.96'
  389.     DB '1.97'
  390.     DB '1.98'
  391.     DB '1.99'
  392.     DB '2.00'
  393.     DB '2.01'
  394.     DB '2.02'
  395.     DB '2.03'
  396.     DB '2.04'
  397.     DB '2.05'
  398.     DB '2.06'
  399.     DB '2.07'
  400.     DB '2.08'
  401.     DB '2.09'
  402.     DB '2.10'
  403.     DB '2.11'
  404.     DB '2.12'
  405.     DB '2.13'
  406.     DB '2.14'
  407.     DB '2.15'
  408.     DB '2.16'
  409.     DB '2.17'
  410.     DB '2.18'
  411.     DB '2.19'
  412.     DB '2.20'
  413.     DB '2.21'
  414.     DB '2.22'
  415.     DB '2.23'
  416.     DB '2.24'
  417.     DB '2.25'
  418.     DB '2.26'
  419.     DB '2.27'
  420.     DB '2.28'
  421.     DB '2.29'
  422.     DB '2.30'
  423.     DB '2.31'
  424.     DB '2.32'
  425.     DB '2.33'
  426.     DB '2.34'
  427.     DB '2.35'
  428.     DB '2.36'
  429.     DB '2.37'
  430.     DB '2.38'
  431.     DB '2.39'
  432.     DB '2.40'
  433.     DB '2.41'
  434.     DB '2.42'
  435.     DB '2.43'
  436.     DB '2.44'
  437.     DB '2.45'
  438.     DB '2.46'
  439.     DB '2.47'
  440.     DB '2.48'
  441.     DB '2.49'
  442.     DB '2.50'
  443. END START
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement