Advertisement
Guest User

Untitled

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