First of all; there should be a main ground cable going from the battery negative (-) terminal to the engine block. The main ground cable must be fastened to a bolt that threads directly into the engine block: It cannot go to a bracket that is then bolted to the engine at another point, or to any part that is not clean, bare metal or is not made of metal. In addition to the main ground cable; there should also be an auxiliary ground cable going either from the battery negative (-) terminal to a bolt in the truck body, or going from the engine block to a bolt in the firewall. If neither of those auxiliary ground cables are found on the truck; it is necessary to buy and install one or both of those cables. If both auxiliary ground cables are missing; this could cause the battery indicator problem and possibly one or both of the codes.
The battery indicator dropping could also mean that the alternator is not charging the battery properly at idle. After the battery ground cables are all in place, the next thing I would do is to have the alternator output at idle speed tested when the fan and lights are on. If there is less than 13.5 volts at the battery when the engine idles with the lights and fan on; the alternator should be replaced. I would not get a rebuilt alternator from most parts stores; because they are often poorly rebuilt and do not work well. NAPA parts stores are the only place I would trust for buying a rebuilt alternator; and even then, I would be sure to get a NAPA premium brand part.
After the alternator charges properly; I would clear the trouble codes with a code scanner, and then see if the codes come back after a few miles of driving (do NOT try to clear codes on this truck by disconnecting the battery; that only works on 1995 and older models. If you disconnect the battery to clear codes on this truck; the codes will go away and then come right back).
If you clear the codes with a scanner, and they still come back; replace the crankshaft position sensor and see if that stops the P0320 code. If that code still comes back after the crank sensor is replaced and the code is cleared; then replace the camshaft position sensor. After that; any further return of the P0320 code would indicate a problem in the electrical wiring at the sensors; or in the wiring harness.
To fix the P0401 code; remove and clean the EGR valve and its connecting passages in the intake manifold, and clear the code with a scanner. Check all the vacuum hoses that go to the EGR valve and any of its other vacuum controls for leaks, cracks, and loose connections. If this does not fix the P0401 code; replace the EGR valve position sensor and clear the code again.