Let \(\ell ^m\) be a power with \(\ell \) a prime greater than 3 and \(m\) a positive integer such that 3 is a primitive root modulo \(2\ell ^m\) . Let \(\mathbb {F}_3\) be the finite field of order 3, and let \(\mathbb {F}\) be the \(\ell ^{m-1}(\ell -1)\) -th extension field of \(\mathbb {F}_3\) . Denote by \(\text {Tr}\) the absolute trace map from \(\mathbb {F}\) to \(\mathbb {F}_3\) . For any \(\alpha \in \mathbb {F}_3\) and \(\beta \in \mathbb {F}\) , let \(D\) be the set of nonzero solutions in \(\mathbb {F}\) to the equation \(\text {Tr}(x^{\frac{q-1}{2\ell ^m}} + \beta x) = \alpha \) . In this paper, we investigate a ternary code \(\mathcal {C}\) of length \(n\) , defined by \(\mathcal {C}:= \{(\text {Tr}(d_1x), \text {Tr}(d_2x), \dots , \text {Tr}(d_nx)): x \in \mathbb {F}\}\) when we rewrite \(D = \{d_1, d_2, \dots , d_n\}\) . Using recent results on explicit evaluations of exponential sums, the Weil bound, and combinatorial techniques, we determine the Hamming weight distribution of the code \(\mathcal {C}\) . Furthermore, we show that when \(\alpha = \beta = 0\) , the dual code of \(\mathcal {C}\) is optimal with respect to the Hamming bound.