spi_prog.rmh

来自「和picoblaze完全兼容的mcu ip core」· RMH 代码 · 共 1,531 行 · 第 1/5 页

RMH
1,531 行
字号
// #937: ;// #938: ;Delay of 1us.// #939: ;// #940: ;Constant value defines reflects the clock applied to KCPSM3. Every instruction// #941: ;executes in 2 clock cycles making the calculation highly predictable. The '6' in// #942: ;the following equation even allows for 'CALL delay_1us' instruction in the initiating code.// #943: ;// #944: ; delay_1us_constant =  (clock_rate - 6)/4       Where 'clock_rate' is in MHz// #945: ;// #946: ;Registers used s0// #947: ;// @197 #948: [delay_1us]0000b // @197 #948: LOAD(s0,delay_1us_constant)// @198 #949: [wait_1us]1c001 // @198 #949: SUB(s0,1)35598 // @199 #950: JUMP(NZ,wait_1us)2a000 // @19a #951: RETURN// #952: ;// #953: ;Delay of 40us.// #954: ;// #955: ;Registers used s0, s1// #956: ;// @19b #957: [delay_40us]00128 // @19b #957: LOAD(s1,40) ;40 x 1us = 40us// @19c #958: [wait_40us]30197 // @19c #958: CALL(delay_1us)1c101 // @19d #959: SUB(s1,1)3559c // @19e #960: JUMP(NZ,wait_40us)2a000 // @19f #961: RETURN// #962: ;// #963: ;// #964: ;Delay of 1ms.// #965: ;// #966: ;Registers used s0, s1, s2// #967: ;// @1a0 #968: [delay_1ms]00219 // @1a0 #968: LOAD(s2,25) ;25 x 40us = 1ms// @1a1 #969: [wait_1ms]3019b // @1a1 #969: CALL(delay_40us)1c201 // @1a2 #970: SUB(s2,1)355a1 // @1a3 #971: JUMP(NZ,wait_1ms)2a000 // @1a4 #972: RETURN// #973: ;// #974: ;Delay of 20ms.// #975: ;// #976: ;Delay of 20ms used during initialisation.// #977: ;// #978: ;Registers used s0, s1, s2, s3// #979: ;// @1a5 #980: [delay_20ms]00314 // @1a5 #980: LOAD(s3,20) ;20 x 1ms = 20ms// @1a6 #981: [wait_20ms]301a0 // @1a6 #981: CALL(delay_1ms)1c301 // @1a7 #982: SUB(s3,1)355a6 // @1a8 #983: JUMP(NZ,wait_20ms)2a000 // @1a9 #984: RETURN// #985: ;// #986: ;Delay of approximately 1 second.// #987: ;// #988: ;Registers used s0, s1, s2, s3, s4// #989: ;// @1aa #990: [delay_1s]00414 // @1aa #990: LOAD(s4,20) ;50 x 20ms = 1000ms// @1ab #991: [wait_1s]301a5 // @1ab #991: CALL(delay_20ms)1c401 // @1ac #992: SUB(s4,1)355ab // @1ad #993: JUMP(NZ,wait_1s)2a000 // @1ae #994: RETURN// #995: ;// #996: ;**************************************************************************************// #997: ;UART communication routines// #998: ;**************************************************************************************// #999: ;// #1000: ;Read one character from the UART// #1001: ;// #1002: ;Character read will be returned in a register called 'UART_data'.// #1003: ;// #1004: ;The routine first tests the receiver FIFO buffer to see if data is present.// #1005: ;If the FIFO is empty, the routine waits until there is a character to read.// #1006: ;As this could take any amount of time the wait loop could include a call to a// #1007: ;subroutine which performs a useful function.// #1008: ;// #1009: ;If the received character is an XOFF, then the routine will then wait// #1010: ;for an XON to be received. This means that the rest of the program is held// #1011: ;in suspense and therefore it can not transmit. Once an XON is received, it will// #1012: ;again wait for a normal character before returning.// #1013: ;// #1014: ;NOTE: Characters between the XOFF and XON will be ignored in this version of the// #1015: ;program!!!// #1016: ;// #1017: ;Interrupt is disabled during this routine to prevent a false situation. If the// #1018: ;receiver half-full flag went High it should result in an interrupt transmitting// #1019: ;an XOFF character. However, if this routine were able to read the receiver buffer// #1020: ;at just about the same as the hardware detects the half-full flag, then it could// #1021: ;think that an XON needs to be transmitted.// #1022: ;// #1023: ;// #1024: ;Registers used s0 and UART_data// #1025: ;// @1af #1026: [read_from_UART]3c000 // @1af #1026: DISABLE(INTERRUPT)// @1b0 #1027: [wait_Rx_character]04000 // @1b0 #1027: INPUT(s0,status_port) ;test Rx_FIFO buffer12008 // @1b1 #1028: TEST(s0,rx_data_present)355b4 // @1b2 #1029: JUMP(NZ,read_character)341b0 // @1b3 #1030: JUMP(wait_Rx_character)// @1b4 #1031: [read_character]04f01 // @1b4 #1031: INPUT(UART_data,UART_read_port) ;read from FIFO14f13 // @1b5 #1032: COMPARE(UART_data,character_XOFF) ;test for XOFF351b9 // @1b6 #1033: JUMP(Z,wait_XON)3c001 // @1b7 #1034: ENABLE(INTERRUPT) ;normal finish2a000 // @1b8 #1035: RETURN// @1b9 #1036: [wait_XON]04000 // @1b9 #1036: INPUT(s0,status_port) ;test Rx_FIFO buffer12008 // @1ba #1037: TEST(s0,rx_data_present)355bd // @1bb #1038: JUMP(NZ,read_XON)341b9 // @1bc #1039: JUMP(wait_XON)// @1bd #1040: [read_XON]04f01 // @1bd #1040: INPUT(UART_data,UART_read_port) ;read from FIFO14f11 // @1be #1041: COMPARE(UART_data,character_XON) ;test for XON351b0 // @1bf #1042: JUMP(Z,wait_Rx_character) ;now wait for normal character341b9 // @1c0 #1043: JUMP(wait_XON) ;continue to wait for XON// #1044: ;// #1045: ;// #1046: ;// #1047: ;Transmit one character to the UART// #1048: ;// #1049: ;Character supplied in register called 'UART_data'.// #1050: ;// #1051: ;The routine first tests the transmit FIFO buffer is empty.// #1052: ;If the FIFO currently has any data, the routine waits until it is empty.// #1053: ;Ultimately this means that only one character is sent at a time which// #1054: ;could be important if the PC at the other end of the link transmits// #1055: ;an XOFF and needs the flow of data to terminate as soon as possible.// #1056: ;// #1057: ;Registers used s0// #1058: ;// @1c1 #1059: [send_to_UART]04000 // @1c1 #1059: INPUT(s0,status_port) ;test Tx_FIFO buffer12001 // @1c2 #1060: TEST(s0,tx_data_present)351c5 // @1c3 #1061: JUMP(Z,UART_write)341c1 // @1c4 #1062: JUMP(send_to_UART)// @1c5 #1063: [UART_write]2cf10 // @1c5 #1063: OUTPUT(UART_data,UART_write_port)2a000 // @1c6 #1064: RETURN// #1065: ;// #1066: ;Convert value provided in register s0 into ASCII characters// #1067: ;// #1068: ;The value provided must in the range 0 to 99 and will be converted into// #1069: ;two ASCII characters.// #1070: ;     The number of 'tens' will be represented by an ASCII character returned in register s1.// #1071: ;     The number of 'units' will be represented by an ASCII character returned in register s0.// #1072: ;// #1073: ;The ASCII representations of '0' to '9' are 30 to 39 hexadecimal which is simply 30 hex added to// #1074: ;the actual decimal value.// #1075: ;// #1076: ;Registers used s0 and s1.// #1077: ;// @1c7 #1078: [decimal_to_ASCII]00130 // @1c7 #1078: LOAD(s1,48) ;load 'tens' counter with ASCII for '0'// @1c8 #1079: [test_for_ten]18101 // @1c8 #1079: ADD(s1,1) ;increment 'tens' value1c00a // @1c9 #1080: SUB(s0,10) ;try to subtract 10 from the supplied value35dc8 // @1ca #1081: JUMP(NC,test_for_ten) ;repeat if subtraction was possible without underflow.1c101 // @1cb #1082: SUB(s1,1) ;'tens' value one less ten due to underflow1803a // @1cc #1083: ADD(s0,58) ;restore units value (the remainder) and convert to ASCII2a000 // @1cd #1084: RETURN// #1085: ;// #1086: ;// #1087: ;// #1088: ;Convert character to upper case// #1089: ;// #1090: ;The character supplied in register s0.// #1091: ;If the character is in the range 'a' to 'z', it is converted// #1092: ;to the equivalent upper case character in the range 'A' to 'Z'.// #1093: ;All other characters remain unchanged.// #1094: ;// #1095: ;Registers used s0.// #1096: ;// @1ce #1097: [upper_case]14061 // @1ce #1097: COMPARE(s0,97) ;eliminate character codes below 'a' (61 hex)2b800 // @1cf #1098: RETURN(C)1407b // @1d0 #1099: COMPARE(s0,123) ;eliminate character codes above 'z' (7A hex)2bc00 // @1d1 #1100: RETURN(NC)0a0df // @1d2 #1101: AND(s0,DF) ;mask bit5 to convert to upper case2a000 // @1d3 #1102: RETURN// #1103: ;// #1104: ;// #1105: ;Convert character '0' to '9' to numerical value in range 0 to 9// #1106: ;// #1107: ;The character supplied in register s0. If the character is in the// #1108: ;range '0' to '9', it is converted to the equivalent decimal value.// #1109: ;Characters not in the range '0' to '9' are signified by the return// #1110: ;with the CARRY flag set.// #1111: ;// #1112: ;Registers used s0.// #1113: ;// @1d4 #1114: [onechar_to_value]180c6 // @1d4 #1114: ADD(s0,C6) ;reject character codes above '9' (39 hex)2b800 // @1d5 #1115: RETURN(C) ;carry flag is set1c0f6 // @1d6 #1116: SUB(s0,F6) ;reject character codes below '0' (30 hex)2a000 // @1d7 #1117: RETURN ;carry is set if value not in range// #1118: ;// #1119: ;// #1120: ;Determine the numerical value of a two character decimal string held in// #1121: ;scratch pad memory such the result is in the range 0 to 99 (00 to 63 hex).// #1122: ;// #1123: ;The string must be stored in two consecutive memory locations and the// #1124: ;location of the first (tens) character supplied in the s1 register.// #1125: ;The result is provided in register s2. Strings not using characters in the// #1126: ;range '0' to '9' are signified by the return with the CARRY flag set.// #1127: ;// #1128: ;Registers used s0, s1 and s2.// #1129: ;// @1d8 #1130: [twochar_to_value]07010 // @1d8 #1130: FETCH(s0,s1) ;read 'tens' character301d4 // @1d9 #1131: CALL(onechar_to_value) ;convert to numerical value2b800 // @1da #1132: RETURN(C) ;bad character - CARRY set01200 // @1db #1133: LOAD(s2,s0)20206 // @1dc #1134: SL0(s2) ;multiply 'tens' value by 10 (0A hex)20206 // @1dd #1135: SL0(s2)19200 // @1de #1136: ADD(s2,s0)20206 // @1df #1137: SL0(s2)18101 // @1e0 #1138: ADD(s1,1) ;read 'units' character07010 // @1e1 #1139: FETCH(s0,s1)301d4 // @1e2 #1140: CALL(onechar_to_value) ;convert to numerical value2b800 // @1e3 #1141: RETURN(C) ;bad character - CARRY set19200 // @1e4 #1142: ADD(s2,s0) ;add units to result and clear CARRY flag2a000 // @1e5 #1143: RETURN// #1144: ;// #1145: ;// #1146: ;Convert hexadecimal value provided in register s0 into ASCII characters// #1147: ;// #1148: ;The value provided must can be any value in the range 00 to FF and will be converted into// #1149: ;two ASCII characters.// #1150: ;     The upper nibble will be represented by an ASCII character returned in register s2.// #1151: ;     The lower nibble will be represented by an ASCII character returned in register s1.// #1152: ;// #1153: ;The ASCII representations of '0' to '9' are 30 to 39 hexadecimal which is simply 30 hex// #1154: ;added to the actual decimal value. The ASCII representations of 'A' to 'F' are 41 to 46// #1155: ;hexadecimal requiring a further addition of 07 to the 30 already added.// #1156: ;// #1157: ;Registers used s0, s1 and s2.// #1158: ;// @1e6 #1159: [hex_byte_to_ASCII]01100 // @1e6 #1159: LOAD(s1,s0) ;remember value supplied2000e // @1e7 #1160: SR0(s0) ;isolate upper nibble2000e // @1e8 #1161: SR0(s0)2000e // @1e9 #1162: SR0(s0)2000e // @1ea #1163: SR0(s0)301f2 // @1eb #1164: CALL(hex_to_ASCII) ;convert01200 // @1ec #1165: LOAD(s2,s0) ;upper nibble value in s201010 // @1ed #1166: LOAD(s0,s1) ;restore complete value0a00f // @1ee #1167: AND(s0,15) ;isolate lower nibble301f2 // @1ef #1168: CALL(hex_to_ASCII) ;convert01100 // @1f0 #1169: LOAD(s1,s0) ;lower nibble value in s12a000 // @1f1 #1170: RETURN// #1171: ;// #1172: ;Convert hexadecimal value provided in register s0 into ASCII character// #1173: ;// #1174: ;Register used s0// #1175: ;// @1f2 #1176: [hex_to_ASCII]1c00a // @1f2 #1176: SUB(s0,10) ;test if value is in range 0 to 9359f5 // @1f3 #1177: JUMP(C,number_char)18007 // @1f4 #1178: ADD(s0,7) ;ASCII char A to F in range 41 to 46// @1f5 #1179: [number_char]1803a // @1f5 #1179: ADD(s0,58) ;ASCII char 0 to 9 in range 30 to 402a000 // @1f6 #1180: RETURN// #1181: ;// #1182: ;// #1183: ;Send the two character HEX value of the register contents 's0' to the UART// #1184: ;// #1185: ;Registers used s0, s1, s2// #1186: ;// @1f7 #1187: [send_hex_byte]301e6 // @1f7 #1187: CALL(hex_byte_to_ASCII)01f20 // @1f8 #1188: LOAD(UART_data,s2)301c1 // @1f9 #1189: CALL(send_to_UART)01f10 // @1fa #1190: LOAD(UART_data,s1)301c1 // @1fb #1191: CALL(send_to_UART)2a000 // @1fc #1192: RETURN// #1193: ;// #1194: ;// #1195: ;// #1196: ;Send the six character HEX value of the register contents [s9,s8,s7] to the UART// #1197: ;// #1198: ;Registers used s0, s1, s2// #1199: ;// @1fd #1200: [send_hex_3bytes]01090 // @1fd #1200: LOAD(s0,s9)301f7 // @1fe #1201: CALL(send_hex_byte)01080 // @1ff #1202: LOAD(s0,s8)301f7 // @200 #1203: CALL(send_hex_byte)01070 // @201 #1204: LOAD(s0,s7)301f7 // @202 #1205: CALL(send_hex_byte)2a000 // @203 #1206: RETURN// #1207: ;// #1208: ;// #1209: ;Convert the HEX ASCII characters contained in 's3' and 's2' into// #1210: ;an equivalent hexadecimal value in register 's0'.// #1211: ;     The upper nibble is represented by an ASCII character in register s3.// #1212: 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?